In order to make mega 16 output pulses, we can use T0, T 1 and T2 of single chip microcomputer to output PWM. T 1 is 16, which is a bit troublesome. Just use T0. You can use the T0 port as the PWM output (specify the port, not every IO). In order to better drive, add a triode at the output end as the pre-driver, and you can use 8050. What the program needs to do is to set the frequency of PWM, which is related to the frequency division of the timer, and the duty ratio setting is related to the initial value and matching value of the timer. I'll give you a little program. Just have a look and modify it. //contains the required header file.
# Contains? & ltIOM 16v . h & gt;
# Contains? & ltmacros.h & gt
/*-Macro Definition-*/
# Definition? Ucal? Not signed? tea
# Definition? uint? Not signed? Internationalorganizations (same as international organizations)
//port initialization
Invalid? Port_init (empty)
{
PORTA? =? 0x00
DDRA? =? 0x00
PORTB? =? 0x00
DDRB? =? 0x00
PORTC? =? 0x00
DDRC? =? 0x00
Porter? =? 0x00
DDRD? =? 0x00
}
//Timer T0 is initialized.
Invalid? Timer 0_init (invalid)
{
TCCR0? =? 0x00// Stop timer
TCNT0? =? 0x00// initial value
OCR0=? 0x 0 1; //matching value, you can change the duty cycle. The larger the value, the higher the duty cycle. You can also change the PWM duty cycle by changing this value in the main function.
Timsk? |=? 0x00// Allow interrupt
TCCR0? =? 0x 6 1; //Start the timer, and set the frequency division here.
}
Invalid? Initialize device (invalid)
{
CLI(); ? //Disable all interrupts
MCUCR? =? 0x00
MCUCSR? =? 0x80// Disable JTAG
GICR=? 0x00
port _ init();
Timer 0 _ init ();
SEI(); //Turn on global interrupt
}
//main function
Invalid? Major (invalid)
{
init _ devices(); //Initialize the service function
while( 1)
{
NOP(); //Change to your program
}
}