ATmega128 ATmega128 gg(8bit AVR Microprocessor)(8bit AVR Microprocessor)
Human-Robot Interaction Class2008. 4. 28 Koosy
12008 Human-Robot Interaction Class
ContentsContentsContentsContentsMicro Controller Unit Overview Micro Controller Unit Overview ATmega128 Features Necessary ToolsNecessary Tools
General I/OGeneral I/OExternal Interrupt8bit/16bit Timer/Counter8bit/16bit Timer/Counter◦ Timer Interrupt◦ PWM Generation◦ PWM Generation
Analog to Digital ConverterUSARTUSART
22008 Human-Robot Interaction Class
Micro Controller UnitMicro Controller UnitMicro Controller UnitMicro Controller Unit
Controller : Controls the flow of information
Arithmetic Logic Unit : +, -, *, /, Shift, Rotate, AND, OR…g
Memory : RAM(SRAM/DRAM…), ROM(Flash, EEPROM…)
I/O : …
2007 TCL Summer AVR Application Study 32008 Human-Robot Interaction Class
ATmega128 FeaturesATmega128 FeaturesATmega128 FeaturesATmega128 FeaturesAVR 8-bit MicrocontrollerATmega128L : 2.7~5.5V, 0~8MHzATmega128 : 4.5~5.5V, 0~16MHz128K Bytes of in-system reprogrammable flash4K Bytes EEPROM4K Bytes EEPROM4K Bytes Internal SRAMUp to 64K Bytes optional external memory space
53 programmable I/O lines8 Channels external interruptTwo 8-bit Timer/CounterTwo 16 bit Timer/CounterTwo 16-bit Timer/Counter6 PWM channels with programmable resolution (2~16 bit)Output compare modulator8-channels, 10-bit ADCDual programmable Serial USART’sMaster/Slave SPI Serial interfaceOn-chip Analog Comparator
42008 Human-Robot Interaction Class
Necessary ToolsNecessary ToolsNecessary ToolsNecessary ToolsFor ProgrammingFor Programming◦ AVRStudio◦ WinAVRWinAVR◦ Codevision
For download and debug◦ ISP : downlodeISP : downlode◦ JTAG : downlode + debug
52008 Human-Robot Interaction Class
General I/OGeneral I/OGeneral I/OGeneral I/O53 Read/Write convertible pin53 Read/Write convertible pinDDRn : Data Direction Register
0 : input / 1 : outputp p
Input◦ DDRn : 0
◦ PORTn : 1 – using pull-up resistor0 – no using pull-up resistor
◦ PINn : Data resisterPINn : Data resister
Output◦ DDRn : 1
◦ PORTn : initial value
62008 Human-Robot Interaction Class
General I/O How to use?General I/O How to use?General I/O How to use?General I/O How to use?
Output PINI i i l V l
p
I t PIN
Initial Value
Pul-up ResisterInput PIN Pul up Resister
2007 TCL Summer AVR Application Study 7
External InterruptExternal InterruptExternal InterruptExternal Interrupt8 channels : INT0~78 channels : INT0 7
Execute some function from external digital inputMode : low level / falling edge / rising edgeMode : low level / falling edge / rising edge
82008 Human-Robot Interaction Class
External Interrupt How to use?External Interrupt How to use?External Interrupt. How to use?External Interrupt. How to use?
Enable Ext IRQ M d S l tEnable Ext IRQ Mode Select
Falling Edge
Rising Edge
9
g g
2008 Human-Robot Interaction Class
External Interrupt How to use?External Interrupt How to use?External Interrupt. How to use?External Interrupt. How to use?// External Interrupt 0 service routine
[ ] d ( d){interrupt [EXT_INT0] void ext_int0_isr(void){
// Place your code here
}
// External Interrupt 1 service routine
i [EXT INT1] id i 1 i ( id){
Function definition
interrupt [EXT_INT1] void ext_int1_isr(void){
// Place your code here
}
// E l I ( ) i i i li i// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: On
// INT1 M d Ri i Ed// INT1 Mode: Rising Edge
// INT2: Off
// INT3: Off
// INT4: Off
// INT5 Off
Resister setting
// INT5: Off
// INT6: Off
// INT7: Off
EICRA=0x0E;
EICRB 0 00
10
EICRB=0x00;
EIMSK=0x03;
EIFR=0x03;
2008 Human-Robot Interaction Class
8bit/16bit Timer/Counter 8bit/16bit Timer/Counter 8bit/16bit Timer/Counter 8bit/16bit Timer/Counter 8bit Timer –Timer0, Timer2◦ 8-bit ◦ Clear Timer on Compare Match(Auto Reload)◦ Phase Correct PWM◦ 10-bit Clock Prescaler◦ Overflow and Compare Match Interrupt Source
16bit Timer – Timer1, Timer3◦ 16-bit ◦ Three independent Output Compare Unit◦ Three independent Output Compare Unit◦ Double Buffered Output Compare Registers◦ One Input Capture Unit
Cl Ti C M t h (A t R l d)◦ Clear Timer on Compare Match (Auto Reload)◦ Phase Correct PWM◦ Variable PWM Period
1 O fl 3 O C 1 I C I S◦ 1 Overflow, 3 Output Compare, 1 Input Capture Interrupt Sources
112008 Human-Robot Interaction Class
88 bit Timer (Timer0 Timer2) bit Timer (Timer0 Timer2) 88--bit Timer (Timer0, Timer2) bit Timer (Timer0, Timer2)
Overflow Interrupt
Output Compare InterruptTimer Speed
8-bit register
PWM Output Pin
122008 Human-Robot Interaction Class
Modes of Operation 1Modes of Operation 1Modes of Operation 1Modes of Operation 1Normal ModeNormal Mode◦ Increase TCNT from Bottom(0x00) to Max(0xff)◦ Overflow Interrupt at Max TCNTOverflow Interrupt at Max TCNT◦ Restart from the Bottom when TCNT reaches to the Max
Clear Timer of Compare Match (CTC)Mode
132008 Human-Robot Interaction Class
Modes of Operation 2Modes of Operation 2Modes of Operation 2Modes of Operation 2Fast PWM ModeFast PWM Mode
142008 Human-Robot Interaction Class
Modes of Operation 3Modes of Operation 3Modes of Operation 3Modes of Operation 3Phase Correct PWM ModePhase Correct PWM Mode
152008 Human-Robot Interaction Class
1616 bit Timer (Timer1 Timer3) bit Timer (Timer1 Timer3) 1616--bit Timer (Timer1, Timer3) bit Timer (Timer1, Timer3) Overflow Interrupt
Timer Speed
O t t C I t t
p
16-bit register
Output Compare Interrupt
PWM Out Pin
162008 Human-Robot Interaction Class
Modes of Operation 1Modes of Operation 1Modes of Operation 1Modes of Operation 1Normal ModeNormal Mode◦ Increase TCNT from Bottom(0x00) to Max(0xff)◦ Overflow Interrupt at Max TCNTOverflow Interrupt at Max TCNT◦ Restart from the Bottom when TCNT reaches to the Max
Clear Timer of Compare Match (CTC)Mode
172008 Human-Robot Interaction Class
Modes of Operation 2Modes of Operation 2Modes of Operation 2Modes of Operation 2Fast PWM ModeFast PWM Mode
182008 Human-Robot Interaction Class
Modes of Operation 3Modes of Operation 3Modes of Operation 3Modes of Operation 3Phase Correct PWM ModePhase Correct PWM Mode
192008 Human-Robot Interaction Class
Modes of Operation 4Modes of Operation 4Modes of Operation 4Modes of Operation 4Phase & Frequency Correct PWM ModePhase & Frequency Correct PWM Mode
202008 Human-Robot Interaction Class
Code Vision SettingCode Vision SettingCode Vision SettingCode Vision Setting
Timer Speed
Mode Selection
Output pin
Interrupt
TCNT
Output Compare Value
212008 Human-Robot Interaction Class
Example 1Example 1Example 1Example 11ms Timer interrupt1ms Timer interrupt◦ Timer : 250kHz
◦ Mode : CTC
◦ OCR0 : 250 (0xfa)
PWM generation ◦ Period : 1ms, Duty : 10%, Fast PWM
1ms1ms0.1ms
◦ Timer : 250kHz
◦ Mode : Fast PWM top=ICR1p
◦ ICR1 = 0x00fa
◦ OCR1 = 0x0019 222008 Human-Robot Interaction Class
CodevisionCodevision Program for Quiz1Program for Quiz1 11CodevisionCodevision Program for Quiz1Program for Quiz1--11// Timer 0 output compare interrupt service routine
interrupt [TIM0_COMP] void timer0_comp_isr(void)
{
// Place your code here
}
// Port B initialization
// Func7=In Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x30;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 250.000 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
ASSR=0x00;
TCCR0=0x04;
TCNT0=0x00;
OCR0=0xFA;
232008 Human-Robot Interaction Class
CodevisionCodevision Program for Quiz1Program for Quiz1 22CodevisionCodevision Program for Quiz1Program for Quiz1--22// Port B initialization
// Func7=In Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x30;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 250.000 kHz
// Mode: Fast PWM top=ICR1
// OC1A output: Non-Inv.
// OC1B output: Discon.
// OC1C output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x82;
TCCR1B=0x1B;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0xFA;
OCR1AH=0x00;
OCR1AL=0x19;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL 0 00
242008 Human-Robot Interaction Class
Example 2Example 2Example 2Example 2Set 16bit Timer for driving IC-servo motorSet 16bit Timer for driving IC servo motor
IC-servo motor specp◦ Pulse duration : 0.9ms~2.1ms. ◦ Pulse refreshes over 50Hz(20ms)
Answer◦ Clock : 2000kHz◦ Clock : 2000kHz◦ Duty : 65535/2000k = 32ms◦ 0.9ms : 1800tic◦ 2.1ms : 4200tic◦ Resolution : 4200-1800 =2400
252008 Human-Robot Interaction Class
CodevisionCodevision Program for Quiz2Program for Quiz2CodevisionCodevision Program for Quiz2Program for Quiz2// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 2000.000 kHz
// Mode: Fast PWM top=ICR1
// OC1A output: Non-Inv.
TCCR1A=0x82;
TCCR1B=0x1A;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0xFF;
ICR1L=0xFF;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;
void main(void)
{
while(1){
//// 0.9ms : 1800 ~ 2.1ms : 4200
// center 1.5ms : 3000
OCR1AH = (3000 >> 8) & 0x00ff;
OCR1AL = 3000 & 0x00ff;
}
262008 Human-Robot Interaction Class
Analog to Digital ConverterAnalog to Digital ConverterAnalog to Digital ConverterAnalog to Digital Converter
8 channels8 channels
10-bit / 8-bit resolution
0 ~ 5v Input Voltage Range
Interrupt on ADC Conversion CompleteInterrupt on ADC Conversion Complete
272008 Human-Robot Interaction Class
Code Vision SettingCode Vision SettingCode Vision SettingCode Vision Setting
282008 Human-Robot Interaction Class
CodevisionCodevision ProgramProgramCodevisionCodevision ProgramProgram#define ADC_VREF_TYPE 0x00
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input|ADC_VREF_TYPE;
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
// ADC initialization
// ADC Clock frequency: 125.000 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x87;
while (1)
{
dunsigned int a;
a= read_adc(1);
};
} 292008 Human-Robot Interaction Class
USARTUSARTUSARTUSART2 channels2 channelsFull duplex operationAsynchronous & Synchronous operation5 6 7 8 or 9 data bits and 1 or 2 stop bits5,6,7,8 or 9 data bits and 1 or 2 stop bitsOdd or Even parity bit 2400~115200 Baud rate
302008 Human-Robot Interaction Class
USART USART CodevisionCodevision SettingSettingUSART USART CodevisionCodevision SettingSetting
312008 Human-Robot Interaction Class
USART USART CodevisionCodevision ProgramProgramUSART USART CodevisionCodevision ProgramProgramchar getchar(void){
char data;
while (rx_counter0==0);
data=rx_buffer0[rx_rd_index0];
if (++rx_rd_index0 == RX_BUFFER_SIZE0) rx_rd_index0=0;
#asm("cli")
--rx_counter0;
#asm("sei")
return data;
}
void putchar(char c){
while (tx_counter0 == TX_BUFFER_SIZE0);
#asm("cli")
if (tx_counter0 || ((UCSR0A & DATA_REGISTER_EMPTY)==0))
{
tx_buffer0[tx_wr_index0]=c;
if (++tx_wr_index0 == TX_BUFFER_SIZE0) tx_wr_index0=0;
++tx_counter0;
}
else
UDR0=c;
#asm("sei")
}
322008 Human-Robot Interaction Class
USART USART CodevisionCodevision ProgramProgramUSART USART CodevisionCodevision ProgramProgram// USART0 Receiver interrupt service routine
interrupt [USART0_RXC] void usart0_rx_isr(void)
{
char status,data;
status=UCSR0A;
data=UDR0;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer0[rx_wr_index0]=data;
if (++rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0;
if (++rx_counter0 == RX_BUFFER_SIZE0)
{
rx_counter0=0;
rx_buffer_overflow0=1;
};
};
}
void main(void)
{
char c;
h l ( ){While(1){
c=getchar();
putchar(c);
}
}
332008 Human-Robot Interaction Class
Serial Packet for multiSerial Packet for multi data data Serial Packet for multiSerial Packet for multi--data data Add start and end byteAdd start and end byteAdd check sum byte
Example Example ◦ Transmit data1~3◦ Start byte : ‘s’, ‘t’◦ End byte : ‘e’, ‘d’y
s t data1 data2 data3 data1+data2+data3 e d
342008 Human-Robot Interaction Class
THANK YOUTHANK YOU
2007 TCL Summer AVR Application Study 35
Top Related