Microprocessor based Design for Biomedical Applications MBE 3
Microprocessor based Design for Biomedical Applications MBE 3 – MDBA III : The ATmega 8 Basic Features (2)
Let‘s repeat some important topics of the last lecture: AVR memories SFRs – GFRs Bit Operations GPIO Ports Clock Sources Interrupt Handling
Today: Fix the IDE – Bug (update AVR-Studio) Repeat blink. c source Use the Simulator Look at avr-gcc, avr-objdump, make ISR – Interrupt Service Routines Timer Interrupt Example 16 -Bit Timer, Modes UART – Configuration and Modes Start with Uart Example
Blink. c – sourcecode review #include <avr/io. h> #include <util/delay_basic. h> Include device specific definitions (SFRs, . . ) #define Bit(x) (1<<x) Macro definition, shift left operation void delay_ms(uint 16_t ms) Function definition, call by value { (call by reference: pointer) uint 16_t t; for (t=0; t<ms; t++) _delay_loop_2(1843); // 1843 * 4 clock cycles gives 7372 cycles: // (approx. 1 millisecond at 7, 3728 MHz } SFRs for Port D configuration int main( void ) { DDRD = Bit(5); // Data. Direction Port D: Pin D 5 = Output ( Led) PORTD= Bit(5); // Switch on led and deactivate pullups on the inputs while (1) { PORTD ^= Bit(5); // toggle D 5 delay_ms(500); // wait 500 milliseconds } return(0); } Bit manipulation Port D (XOR)
Using the AVRStudio - Simulator additional information to the Getting_Started. pdf
AVRStudio Simulator Overview Flow Control Peripherals CPU status Peripheral Details
AVRStudio Simulator Overview Breakpoint Watch Window
AVRStudio Simulator Overview Register and Memory Views Watch Window Disassembly Window
AVRStudio Simulator notes ● Select the Crystal / Oscillator frequency to get accurate time measurements in the Simulator ● beware of delay loops (they take long to simulate) ● use „run to cursor“ or breakpoints for getting into interrupt handlers ● on-chip debugging is useful, needs devices with JTAG interface or debug. Wire interface
Using the Win. AVR-Toolchain from commandline
Win. AVR-Toolchain – avr-gcc. exe Compile Link Convert. elf to. ihex Download to Device. elf: Executable and Linking Format, segment and section information
Win. AVR-Toolchain – make. exe SRC = blink. c test. c CFLAGS= -O 2 -g -Wall MAKEFILE Variables & constants PRG = $(SRC: . c=. hex) all: $(PRG) Target # Create. hex files from. o files %. hex : %. o echo Creating $@ avr-objcopy -O ihex $< $@ Rules # Compile %. o : %. c echo Compiling $@ avr-gcc $(CFLAGS) -mmcu=atmega 8 $< -o $@ clean: rm -f *~ *. bak *. lst *. o *. hexc The makefile automates the build process only changed modules are compiled Environment variables add flexibility current target current prerequistite Browse the GNU MAKE User Manual: http: //www. gnu. org/software/make
Win. AVR-Toolchain – avr-objdump. exe Avr-objdump: Extract information from. elf – file Here: show disassembly information
Where to find AVR C - specific information ? ● AVR-GCC is ANSI-C compatible ● browse the AVR Libc – Documentation ● examine module header files (#include <…>) subdirectory avr/include ● use example code, play with open source firmware
Where to find AVR C - specific information ? <assert. h>: Diagnostics <ctype. h>: Character Operations Useful modules, <errno. h>: System Errors all documented in <inttypes. h>: Integer Type conversions the AVR-Libc manual <math. h>: Mathematics <stdint. h>: Standard Integer Types <stdio. h>: Standard IO facilities <stdlib. h>: General utilities <string. h>: Strings, memory functions <avr/boot. h>: Bootloader Support Utilities <avr/eeprom. h>: EEPROM handling <avr/interrupt. h>: Interrupts <avr/pgmspace. h>: Program Space Utilities <avr/power. h>: Power Reduction Management <avr/io. h>: Special function registers and IO definitions <avr/sleep. h>: Power Management and Sleep Modes <util/delay_basic. h>: Basic busy-wait delay loops <util/parity. h>: Parity bit generation <util/twi. h>: TWI bit mask definitions !
Where to find AVR C - specific information ? Standard integer and character types, defined in <stdint. h>: 8, 16, 32 and 64 bit, signed and unsigned • typedef signed char int 8_t • typedef unsigned char uint 8_t • typedef signed int 16_t • typedef unsigned int uint 16_t • typedef signed long int 32_t • typedef unsigned long int uint 32_t • typedef signed long int 64_t • typedef unsigned long int uint 64_t #define INT 8_MAX 0 x 7 f #define INT 8_MIN (-INT 8_MAX - 1) … Constants for min and max values
Where to find AVR C - specific information ? Some functions from <string. h>: • void • char … memcpy (void , const void , size_t) memmove (void , const void , size_t) strcat (char , const char ) strstr (const char , const char ) strupr (char )
Interrupt handling with the AVR-Libc API Interrupt and Signal definitions: <avr/interrupt. h> ● remember: vector table points to interrupt routines ● by using the appropriate name, your routine will be installed in the table and called when the interrupt occurs ● a set of default interrupt routines is provided ● Macro for registering interrupt routines: ISR (<signalname>) ● saving and restoring registers is handled
Interrupt handling with the AVR-Libc API Examle interrupt handler for the ADC: #include <avr/interrupt. h> ISR(ADC_vect) { // user code here } ● The interrupt source, here the ADC, has to be configured and enabled using SFRs ● Global interrupt instructions: sei () … set interupt enable (I – bit in SREG) cli () … clear interrupt enable ● AVR hardware clears the global interrupt flag before entering an interrupt vector -> use sei() to enable nested interrupts
Interrupt handling with the AVR-Libc API Useful Interrupt vector names for ATmega 8: ADC_vect ANA_COMP_vect EE_RDY_vect INT 0_vect TIMER 0_OVF_vect TIMER 1_CAPT_vect SPI_STC_vect USART_UDRE_vect USART_RXC_vect analog/digital converter analog comparator eeprom ready external interrupt 0 Timer 0 overflow event Timer 1 capture event serial transfer complete USART data register empty USART receive complete
more AVR Peripheral Features
16 – bit Timer / Counter 1 ● Two Independent Output Compare Units ● can be used as Pulse Width Modulator (PWM) ● Clear Timer on Compare Match (Auto Reload) ● One Input Capture Unit ● can be used as External Event Counter ● Four Independent Interrupt Sources TOV 1, OCF 1 A, OCF 1 B, and ICF 1
16 – bit Timer / Counter 1 ● TCNT 1, OCR 1 A, OCR 1 B and ICR 1 are 16 -bit registers ● write high byte first read low byte first (automatically done in C) ● clocking via prescaler or external clock on pin T 1 ● output compare register OCR 1 A/B can be used for PWM generation (output pin: OC 1 A/B) or for interrupt request
16 – bit Timer / Counter 1 ● Input capture Unit: timestamp external events on ICP 1 pin (or on Analog comp. ) Can generate Input Capture Interrupt
16 – bit Timer / Counter 1 ● Output Compare Units: compare TCNT 1 with OCR 1 A and OCR 1 B Can generate Output Capture Interrupt ● 13 PWM / Waveform generation modes ● Top and Bottom values ● Auto reload
16 – bit Timer / Counter 1 ● Mode configuration using TCCR 1 A and TCCR 1 B PWM Mode, set/clear/toggle of OC 1 A/B, auto-reload, Noise canceller Find Details in the ATmega 8 datasheet !
16 – bit Timer / Counter 1 ● Clock source configuration using TCCR 1 B
The UART Serial Interface
USART interface ● universal synchronous / asynchronous receiver / transmitter ● Full Duplex Operation: Receive and Transmit Registers ● Asynchronous or Synchronous Operation ● Frames with 5, 6, 7, 8, or 9 Databits and 1 or 2 Stop Bits ● Odd or Even Parity Generation and Parity Check ● Framing Error Detection, Noise Filtering ● Interrupts possible on TX Complete, TX Data Register Empty and RX Complete
USART interface ● synchronous mode: Pin XCK is used as clock Input (slave) or clock output (master) ● asynchronous mode: receiver and transmitter are clocked independently
USART interface ● Frame formats:
USART interface ● calculating the baud rate register value: low and high byte of ubrr are written into the UBRRL and UBRRH registers Accuracy depends on System clock source ! (see table in ATmega 8 data sheet, pp 155)
USART interface ● Initialisation: set baud rate, frame format, enable TX and RX RXC: RX complete TXC: TX complete UDR: Uart Data Register empty FE: Frame Error DOR: Data Over. Run PE: Parity Error U 2 X: Double the USART speed
USART interface ● Initialisation: set baud rate, frame format, enable TX and RX RXCIE: RX complete interrupt enable TXCIE: TX complete interrupt enable UDRIE: Uart Data Register empty interrupt enable RXEN: Receiver Enable TXEN: Transmitter Enable UCSZ 2: Character Size RXB 8, TXB 8: Bit 8 for receive and transmit
USART interface ● Initialisation: set baud rate, frame format, enable TX and RX URSEL: Register Select (1=UCSR/0=UBRRH) UMSEL: 0=async. mode, 1=sync. Mode UMP 1, UMP 0: Parity mode: 00 = disabled, 10 = even, 11=odd USBS: Stop Bits: 0=1 Stop Bit, 1= 2 Stop bits UCSZ 2, 1, 0 : character size
USART interface character size selection using the UCSZ bits
USART interface Selection of the baud rate using the UBRRH and UBRRL SFRs: URSEL has to be 0 when writing to the UBRRH register
USART interface ● Initialisation: set baud rate, frame format, enable TX and RX void USART_Init( unsigned int baud ) { /* Set baud rate */ UBRRH = (unsigned char)(baud>>8); UBRRL = (unsigned char)baud; /* Set frame format: 8 data, 2 stop bit */ UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ 0); /* Enable Receiver and Transmitter */ UCSRB = (1<<RXEN)|(1<<TXEN); }
USART interface ● Sending bytes in polling mode void USART_Transmit( unsigned char data ) { /* Wait for empty transmit buffer */ while ( !( UCSRA & (1<<UDRE)) ); /* Put data into buffer, sends the data */ UDR = data; }
USART interface ● Receiving bytes in polling mode unsigned char USART_Receive( void ) { /* Wait for data to be received */ while ( !(UCSRA & (1<<RXC)) ) ; /* Get and return received data from buffer */ return UDR; }
- Slides: 40