Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microprocessor based Design for Biomedical Applications MBE 3 – MDBA III : The ATmega8 Basic Features (2)

Similar presentations


Presentation on theme: "Microprocessor based Design for Biomedical Applications MBE 3 – MDBA III : The ATmega8 Basic Features (2)"— Presentation transcript:

1 Microprocessor based Design for Biomedical Applications MBE 3 – MDBA III : The ATmega8 Basic Features (2)

2 Let‘s repeat some important topics of the last lecture: AVR memories SFRs – GFRs Bit Operations GPIO Ports Clock Sources Interrupt Handling

3 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

4 Blink.c – sourcecode review #include #define Bit(x) (1<<x) void delay_ms(uint16_t ms) { uint16_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 } int main( void ) { DDRD = Bit(5); // DataDirection Port D: Pin D5 = Output (Led) PORTD= Bit(5); // Switch on led and deactivate pullups on the inputs while (1) { PORTD ^= Bit(5); // toggle D5 delay_ms(500); // wait 500 milliseconds } return(0); } Macro definition, shift left operation Function definition, call by value (call by reference: pointer) SFRs for Port D configuration Bit manipulation Port D (XOR) Include device specific definitions (SFRs,..)

5 Using the AVRStudio - Simulator additional information to the Getting_Started.pdf

6 Flow Control Peripherals Peripheral Details CPU status AVRStudio Simulator Overview

7 AVRStudio Simulator Overview Breakpoint Watch Window

8 AVRStudio Simulator Overview Register and Memory Views Watch Window Disassembly Window

9 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 debugWire interface

10 Using the WinAVR-Toolchain from commandline

11 WinAVR-Toolchain – avr-gcc.exe Compile Link Convert.elf to.ihex Download to Device.elf: Executable and Linking Format, segment and section information

12 WinAVR-Toolchain – make.exe MAKEFILE 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 Rules Variables & constants Target SRC = blink.c test.c CFLAGS= -O2 -g -Wall PRG = $(SRC:.c=.hex) all: $(PRG) # Create.hex files from.o files %.hex : %.o echo Creating $@ avr-objcopy -O ihex $< $@ # Compile %.o : %.c echo Compiling $@ avr-gcc $(CFLAGS) -mmcu=atmega8 $< -o $@ clean: rm -f *~ *.bak *.lst *.o *.hexc

13 WinAVR-Toolchain – avr-objdump.exe Avr-objdump: Extract information from.elf – file Here: show disassembly information

14 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

15 Where to find AVR C - specific information ? : Diagnostics : Character Operations : System Errors : Integer Type conversions : Mathematics : Standard Integer Types : Standard IO facilities : General utilities : Strings, memory functions : Bootloader Support Utilities : EEPROM handling : Interrupts : Program Space Utilities : Power Reduction Management : Special function registers and IO definitions : Power Management and Sleep Modes : Basic busy-wait delay loops : Parity bit generation : TWI bit mask definitions Useful modules, all documented in the AVR-Libc manual !

16 Where to find AVR C - specific information ? typedef signed char int8_t typedef unsigned char uint8_t typedef signed int int16_t typedef unsigned int uint16_t typedef signed long int int32_t typedef unsigned long int uint32_t typedef signed long long int int64_t typedef unsigned long long int uint64_t #define INT8_MAX 0x7f #define INT8_MIN (-INT8_MAX - 1) … Standard integer and character types, defined in : 8, 16, 32 and 64 bit, signed and unsigned Constants for min and max values

17 Where to find AVR C - specific information ? void memcpy (void, const void, size_t) void memmove (void, const void, size_t) char strcat (char, const char ) char strstr (const char, const char ) char strupr (char ) … Some functions from :

18 Interrupt handling with the AVR-Libc API Interrupt and Signal definitions: ● 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 ( ) ● saving and restoring registers is handled

19 Interrupt handling with the AVR-Libc API Examle interrupt handler for the ADC: #include 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

20 Interrupt handling with the AVR-Libc API Useful Interrupt vector names for ATmega8: ADC_vect analog/digital converter ANA_COMP_vect analog comparator EE_RDY_vect eeprom ready INT0_vect external interrupt 0 TIMER0_OVF_vect Timer0 overflow event TIMER1_CAPT_vect Timer1 capture event SPI_STC_vect serial transfer complete USART_UDRE_vect USART data register empty USART_RXC_vect USART receive complete

21 more AVR Peripheral Features

22 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 TOV1, OCF1A, OCF1B, and ICF1

23 16 – bit Timer / Counter 1 ● TCNT1, OCR1A, OCR1B and ICR1 are 16-bit registers ● write high byte first read low byte first (automatically done in C) ● clocking via prescaler or external clock on pin T1 ● output compare register OCR1A/B can be used for PWM generation (output pin: OC1A/B) or for interrupt request

24 16 – bit Timer / Counter 1 ● Input capture Unit: timestamp external events on ICP1 pin (or on Analog comp.) Can generate Input Capture Interrupt

25 16 – bit Timer / Counter 1 ● Output Compare Units: compare TCNT1 with OCR1A and OCR1B Can generate Output Capture Interrupt ● 13 PWM / Waveform generation modes ● Top and Bottom values ● Auto reload

26 16 – bit Timer / Counter 1 ● Mode configuration using TCCR1A and TCCR1B PWM Mode, set/clear/toggle of OC1A/B, auto-reload, Noise canceller Find Details in the ATmega8 datasheet !

27 16 – bit Timer / Counter 1 ● Clock source configuration using TCCR1B

28 The UART Serial Interface

29 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

30 USART interface ● synchronous mode: Pin XCK is used as clock Input (slave) or clock output (master) ● asynchronous mode: receiver and transmitter are clocked independently

31 USART interface ● Frame formats:

32 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 ATmega8 data sheet, pp 155)

33 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 OverRun PE: Parity Error U2X: Double the USART speed

34 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 UCSZ2: Character Size RXB8, TXB8: Bit 8 for receive and transmit

35 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 UMP1, UMP0: Parity mode: 00 = disabled, 10 = even, 11=odd USBS: Stop Bits: 0=1 Stop Bit, 1= 2 Stop bits UCSZ2,1, 0 : character size

36 USART interface character size selection using the UCSZ bits

37 USART interface Selection of the baud rate using the UBRRH and UBRRL SFRs: URSEL has to be 0 when writing to the UBRRH register

38 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: 8data, 2stop bit */ UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0); /* Enable Receiver and Transmitter */ UCSRB = (1<<RXEN)|(1<<TXEN); }

39 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; }

40 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; }


Download ppt "Microprocessor based Design for Biomedical Applications MBE 3 – MDBA III : The ATmega8 Basic Features (2)"

Similar presentations


Ads by Google