ATmega16KCS
20221007.5
Continue from: Programming Microcontrollers: ATMEGA16A (programmingmicrocontrollerskohcs.blogspot.com)
IAR Embedded Workbench for Microchip AVR 7.30.5
AVRDUDESS 2.14
20221008.6
This would be a rather straight forward set up for the hardware.
Although it is a continuation, some rewriting may be useful.
IAR
Embedded Workbench
Create a New C main Project
Select ATmega16 as the processor
Select ‘Other’ for the linker output
Make
the active project
AVRDUDESS
Select the appropriate programmer
Detect the MCU
Open the ‘.90’ file produced by the Workbench
Flash the file to the ATmega16
Some 100+ bytes would be
written.
20221009.7
The following program would toggle an LED (with a series resistor) connected to Port A bit 0.
// AT01LEDt Toggle Port A bit 0 iRC 1 MHz
#include <iom16.h>
int main( void )
{
int i;
PORTA &= ~0x01 ; // clear Bit 0
DDRA |= 0x01 ; // Bit 0 Output
while(1){
for(i=0;i<=30000;i++){;} // Do not exceed 32767
PORTA |= 0x01 ; // set Bit 0
for(i=0;i<=30000;i++){;}
PORTA &= ~0x01 ; // clear Bit 0
}
}
This is assuming that the MCU is running at 1-MHz with the default Fuse Low Byte of 0xE1.
The Fuse Low Byte can be written with 0xE4 to change the clock to 8 MHz.
The LED would then blink rapidly.
To blink the LED more slowly, the software delay has to be lengthened.
Alternatively, timer interrupt can be used.
20221009.7b
The following program uses Timer 1 to produce a 0.5-second interrupt. (8000000/256=31250)
On interrupt, the LED would toggle, lighting up at the rate of 1 Hz approximately.
// AT03T1OCIA Timer1 OC Interrupt A iRC 8 MHz
#include <iom16.h>
#include <intrinsics.h>
int c;
int main( void )
{
PORTA &= ~0x01 ; // clear Bit 0
DDRA |= 0x01 ; // Bit 0 Output
__disable_interrupt();
TCCR1A = 0x00 ; // Normal
TCCR1B = 0x0C ; // CTC1, prescaler 256
TIMSK |= 0x10 ; // 0x10 is OCIE1A
OCR1A = 31250/2 ; // 0.5-second interrupt
__enable_interrupt();
c = 0 ;
while(1);
}
/* Interrupt handler for the TIMER1_COMPARE vector. */
#pragma vector=TIMER1_COMPA_vect
__interrupt void T1OCA_Handler(void){
if(c==1){
PORTA |= 0x01 ;
c = 0 ;
}
else {
PORTA &= ~0x01 ;
c = 1;
}
}
202210
Comments
Post a Comment