Loading Session2-LF/PWM-LF.c +4 −7 Original line number Diff line number Diff line Loading @@ -7,6 +7,10 @@ #include <avr/interrupt.h> /** In LF, we use the OLDED Xplained extension bhoard to pwm two leds at the same time. Can they do that? */ /* In the pin definitions below, we have defined the PORT as well. Everything works the same way as before, except you have to change PORTx with the PORT definition. Loading @@ -24,13 +28,6 @@ Example: LED1_PORT.OUTSET = (1 << LED1); #define SW1 2 //pin #define SW2 1 //pin #define SW3 2 //pin #define SW1_PORT PORTC //port #define SW2_PORT PORTD //port #define SW3_PORT PORTD //port #define TOP_PERIOD 0x55 Loading Session2-LF/TIMER-LF.c +6 −6 Original line number Diff line number Diff line Loading @@ -12,9 +12,9 @@ #include <util/delay.h> #include <avr/interrupt.h> #define LED1 0 // port B #define LED2 1 // port B #define LED3 4 // Port B #define LED1 4 // port D #define LED2 5 // port D int main(void) Loading @@ -23,8 +23,8 @@ int main(void) First set up the LEDs like in the previous exercise. You can either copy paste from the previous exercise or start fresh. */ PORTB.DIR = (1 << LED1) | (1 << LED2); PORTB.OUTTGL = (1 << LED1); //Starting with only 1 LED on PORTD.DIR = (1 << LED1) | (1 << LED2); PORTD.OUTTGL = (1 << LED1); //Starting with only 1 LED on /*We will be using timer A that will trigger an overflow interupt. This is a 16 bit timer that can run in 2 modes Loading Loading @@ -61,7 +61,7 @@ int main(void) ISR(TCA0_OVF_vect){ //Do something with the led(s), like toggle. PORTB.OUTTGL = (1 << LED1) | (1 << LED2); PORTD.OUTTGL = (1 << LED1) | (1 << LED2); //Clear the interrupt flag. //If we do not clear the flag, we will instantly jump back into the ISR again Loading Session2-LF/UART-LF.c +41 −22 Original line number Diff line number Diff line Loading @@ -5,8 +5,8 @@ * Author : Petter */ #define F_CPU 3333333UL #define BAUD_9600 ((4UL*F_CPU)/9600) #define F_CPU 3333333 #define BAUD_9600 4*F_CPU/9600 #include <avr/io.h> #include <avr/interrupt.h> Loading @@ -15,43 +15,57 @@ /* In this exercise we will set up and use UART communication. The embedded debugger has a virtual com port that we will use to communicate with the computer. */ #define TX_PORT PORTB #define TX_PIN 0 void uart_init(unsigned long baud){ //From chapter 24.3 in datasheet PORTB.DIRSET = (1 << PIN2_bp); //Setting up TX pin as output PORTB.OUTSET = (1 << PIN2_bp); //Setting the TX pin high //From chapter 22.3.1 in datasheet USART0.BAUDH = (baud >> 8); //Shift register right by 8 bits to get the 8 high bits USART0.BAUDL = baud; //Set baud rate without shifting to get the 8 low bits TX_PORT.OUTSET = (1 << TX_PIN); //Setting up TX pin as output TX_PORT.DIRSET = (1 << TX_PIN); //Setting up TX pin as output //It turns out the compiler can handle this automatically, meaning this works just as well: //USART0.BAUD = baud; //Set baud rate register USART3.BAUDL = (uint8_t) baud; //Set baud rate without shifting to get the 8 low bits USART3.BAUDH = (uint8_t)(baud >> 8); //Shift register right by 8 bits to get the 8 high bits //USART.CTRLC CMODE bits default to async, 1 stop bit, 8 bit character size //Since all bits are default 0 we only need to change the character size part USART3.CTRLC = (0x3 << USART_CHSIZE0_bp); USART0.CTRLB |= (1 << USART_RXEN_bp) | (1 << USART_TXEN_bp); //Enable RX and TX //Enable RX and TX USART3.CTRLB = (1 << USART_RXEN_bp) | (1 << USART_TXEN_bp); USART0.CTRLA |= (1 << USART_RXCIE_bp); //Enable interrupts on incoming data //Enable interrupts on incoming data USART3.CTRLA |= (1 << USART_RXCIE_bp); } // function to transmit data void uart_transmit(unsigned char data){ void uart_transmit(char data){ //In this function we will be send data. //First we should check that there isn't already data being sent // if there is, we should probably wait for it to finish first while (!(USART0.STATUS & (1 << USART_DREIF_bp))){ while (!(USART3.STATUS & (1 << USART_DREIF_bp))){ //wait for previous transmit to finish }; //Put our new data into se sending register USART0.TXDATAL = data; //Put our new data into tx data register USART3.TXDATAL = data; } //To send a string we can do this void uart_transmit_string(char* data) { while (*data != '\0') { uart_transmit(*data); data++; } } Loading @@ -67,16 +81,21 @@ int main(void) { //We don't really need to do anything here. //the ISR will handle receiving. uart_transmit_string("Dette er en test\n"); _delay_ms(200); } } //Interrupt service routine for the receiver. ISR(USART0_RXC_vect){ ISR (USART3_RXC_vect) { //In the interrupt we will read the data in the receive buffer //Read out the received data to a variable uint8_t data = USART0_RXDATAL; //Do things with data: uart_transmit(data + 1); //Example: Shift all characters by one step A -> B, B -> C etc. //First we should check that new data has arrived the receive buffer while (!(USART3.STATUS & (1 << USART_RXCIF_bp))){ //wait for previous transmit to finish }; //Store the data in a temporarily variable uint8_t tmp = USART3.RXDATAL; } No newline at end of file Session2/PWM/main.c +0 −1 Original line number Diff line number Diff line Loading @@ -10,7 +10,6 @@ #define LED1 4 // port D, connected to WO4 #define LED2 5 // port D, connected to WO5 #define SW0 2 //on port C Loading Session2/Timer_old/main.c→Session2/Timer/main.c +3 −3 Original line number Diff line number Diff line Loading @@ -7,9 +7,9 @@ #include <util/delay.h> #include <avr/interrupt.h> #define LED1 0 // port B #define LED2 1 // port B #define LED3 4 // Port B #define LED1 4 // port D #define LED2 5 // port D int main(void) Loading Loading
Session2-LF/PWM-LF.c +4 −7 Original line number Diff line number Diff line Loading @@ -7,6 +7,10 @@ #include <avr/interrupt.h> /** In LF, we use the OLDED Xplained extension bhoard to pwm two leds at the same time. Can they do that? */ /* In the pin definitions below, we have defined the PORT as well. Everything works the same way as before, except you have to change PORTx with the PORT definition. Loading @@ -24,13 +28,6 @@ Example: LED1_PORT.OUTSET = (1 << LED1); #define SW1 2 //pin #define SW2 1 //pin #define SW3 2 //pin #define SW1_PORT PORTC //port #define SW2_PORT PORTD //port #define SW3_PORT PORTD //port #define TOP_PERIOD 0x55 Loading
Session2-LF/TIMER-LF.c +6 −6 Original line number Diff line number Diff line Loading @@ -12,9 +12,9 @@ #include <util/delay.h> #include <avr/interrupt.h> #define LED1 0 // port B #define LED2 1 // port B #define LED3 4 // Port B #define LED1 4 // port D #define LED2 5 // port D int main(void) Loading @@ -23,8 +23,8 @@ int main(void) First set up the LEDs like in the previous exercise. You can either copy paste from the previous exercise or start fresh. */ PORTB.DIR = (1 << LED1) | (1 << LED2); PORTB.OUTTGL = (1 << LED1); //Starting with only 1 LED on PORTD.DIR = (1 << LED1) | (1 << LED2); PORTD.OUTTGL = (1 << LED1); //Starting with only 1 LED on /*We will be using timer A that will trigger an overflow interupt. This is a 16 bit timer that can run in 2 modes Loading Loading @@ -61,7 +61,7 @@ int main(void) ISR(TCA0_OVF_vect){ //Do something with the led(s), like toggle. PORTB.OUTTGL = (1 << LED1) | (1 << LED2); PORTD.OUTTGL = (1 << LED1) | (1 << LED2); //Clear the interrupt flag. //If we do not clear the flag, we will instantly jump back into the ISR again Loading
Session2-LF/UART-LF.c +41 −22 Original line number Diff line number Diff line Loading @@ -5,8 +5,8 @@ * Author : Petter */ #define F_CPU 3333333UL #define BAUD_9600 ((4UL*F_CPU)/9600) #define F_CPU 3333333 #define BAUD_9600 4*F_CPU/9600 #include <avr/io.h> #include <avr/interrupt.h> Loading @@ -15,43 +15,57 @@ /* In this exercise we will set up and use UART communication. The embedded debugger has a virtual com port that we will use to communicate with the computer. */ #define TX_PORT PORTB #define TX_PIN 0 void uart_init(unsigned long baud){ //From chapter 24.3 in datasheet PORTB.DIRSET = (1 << PIN2_bp); //Setting up TX pin as output PORTB.OUTSET = (1 << PIN2_bp); //Setting the TX pin high //From chapter 22.3.1 in datasheet USART0.BAUDH = (baud >> 8); //Shift register right by 8 bits to get the 8 high bits USART0.BAUDL = baud; //Set baud rate without shifting to get the 8 low bits TX_PORT.OUTSET = (1 << TX_PIN); //Setting up TX pin as output TX_PORT.DIRSET = (1 << TX_PIN); //Setting up TX pin as output //It turns out the compiler can handle this automatically, meaning this works just as well: //USART0.BAUD = baud; //Set baud rate register USART3.BAUDL = (uint8_t) baud; //Set baud rate without shifting to get the 8 low bits USART3.BAUDH = (uint8_t)(baud >> 8); //Shift register right by 8 bits to get the 8 high bits //USART.CTRLC CMODE bits default to async, 1 stop bit, 8 bit character size //Since all bits are default 0 we only need to change the character size part USART3.CTRLC = (0x3 << USART_CHSIZE0_bp); USART0.CTRLB |= (1 << USART_RXEN_bp) | (1 << USART_TXEN_bp); //Enable RX and TX //Enable RX and TX USART3.CTRLB = (1 << USART_RXEN_bp) | (1 << USART_TXEN_bp); USART0.CTRLA |= (1 << USART_RXCIE_bp); //Enable interrupts on incoming data //Enable interrupts on incoming data USART3.CTRLA |= (1 << USART_RXCIE_bp); } // function to transmit data void uart_transmit(unsigned char data){ void uart_transmit(char data){ //In this function we will be send data. //First we should check that there isn't already data being sent // if there is, we should probably wait for it to finish first while (!(USART0.STATUS & (1 << USART_DREIF_bp))){ while (!(USART3.STATUS & (1 << USART_DREIF_bp))){ //wait for previous transmit to finish }; //Put our new data into se sending register USART0.TXDATAL = data; //Put our new data into tx data register USART3.TXDATAL = data; } //To send a string we can do this void uart_transmit_string(char* data) { while (*data != '\0') { uart_transmit(*data); data++; } } Loading @@ -67,16 +81,21 @@ int main(void) { //We don't really need to do anything here. //the ISR will handle receiving. uart_transmit_string("Dette er en test\n"); _delay_ms(200); } } //Interrupt service routine for the receiver. ISR(USART0_RXC_vect){ ISR (USART3_RXC_vect) { //In the interrupt we will read the data in the receive buffer //Read out the received data to a variable uint8_t data = USART0_RXDATAL; //Do things with data: uart_transmit(data + 1); //Example: Shift all characters by one step A -> B, B -> C etc. //First we should check that new data has arrived the receive buffer while (!(USART3.STATUS & (1 << USART_RXCIF_bp))){ //wait for previous transmit to finish }; //Store the data in a temporarily variable uint8_t tmp = USART3.RXDATAL; } No newline at end of file
Session2/PWM/main.c +0 −1 Original line number Diff line number Diff line Loading @@ -10,7 +10,6 @@ #define LED1 4 // port D, connected to WO4 #define LED2 5 // port D, connected to WO5 #define SW0 2 //on port C Loading
Session2/Timer_old/main.c→Session2/Timer/main.c +3 −3 Original line number Diff line number Diff line Loading @@ -7,9 +7,9 @@ #include <util/delay.h> #include <avr/interrupt.h> #define LED1 0 // port B #define LED2 1 // port B #define LED3 4 // Port B #define LED1 4 // port D #define LED2 5 // port D int main(void) Loading