






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







//file : mylab2.c //author : R. Traylor //date : 7.28. //modified : 7.29. //This code implements lab 2 for ECE473, fall 2005
// PORTA is connected to the shared segments of the LED display. // PORTA.0 corresponds to seg a, PORTA.1 corresponds to seg b, etc. // PORTB bits 4-7 correspond to LED digits 0-3. // Switch 0: toggle function, amount to increment or decrement count. // Switch 1: toggle function, selects which encoder (0,1) changes count. // Switch 2: toggle function, sets display to bright or dim. // Enocder pinout: // encoder 0, A = PORTE. // B = PORTE. // encoder 1, A = PORTE. // B = PORTE.
#include <avr/io.h> #include <avr/delay.h> #include lcd.h //holds function prototypes for lcd.c
int main() { DDRA = 0xFF; //set port A to all outputs DDRB = 0xF0; //set port bits 4-7 B as outputs DDRD = 0x00; //set port D to all inputs DDRE = 0x00; //set port E to all inputs PORTE= 0xFF; //set port E to all pullups PORTB= 0x00; //set port B to all zeros PORTA= 0x00; //set port A to all zeros //main while loop of program follows // -checks encoders // -increment/decrement count if something changed // -display next digit // -check increment/decrement amount and encoder to check while(1){ _delay_loop_2(500); //loop debounce required
/* bound_count / /Keeps the count from going below zero and above 999. / /Takes a 16 bit unsigned int and returns the same. */ /************************************************************************/ int16_t bound_count(int16_t in_value) { if(in_value < 0) return 0; if(in_value > 999) return 999; return in_value; } //bound_count
// from code for a mega void spi_init(void){ DDRB |= (1 << PB2) | (1 << PB3) | (1 << PB5); //Turn on SS, MOSI, SCLK SPCR=(1<<SPE) | (1<<MSTR); //enbl SPI, MSB 1st, init clk as low SPSR=(1<<SPI2X); //SPI at 2x speed (0.5 MHz) }//spi_init //from code for a mega void spi_init(void){ DDRB |= (1<<PB2) | (1<<PB1) | (1<<PB0); //Turn on SS, MOSI, SCLK SPCR=(1<<SPE) | (1<<MSTR); //enbl SPI, clk low init, rising edge sample SPSR=(1<<SPI2X); //SPI at 2x speed (8 MHz) }//spi_init
/* ATmega128 SPI Control Register / #define SPCR _SFR_IO8(0x0D) / SPI Control Register - SPCR / #define SPIE 7 #define SPE 6 #define DORD 5 #define MSTR 4 #define CPOL 3 #define CPHA 2 #define SPR1 1 #define SPR0 0 from iomx8.h (included from iom48.h) also in (.../avr/avr/include/avr) #define SPCR _SFR_IO8 (0x2C) / SPCR */ #define SPIE 7 #define SPE 6 #define DORD 5 #define MSTR 4 #define CPOL 3 #define CPHA 2 #define SPR1 1 #define SPR0 0