100%(2)100% нашли этот документ полезным (2 голоса)
50 просмотров5 страниц
The document contains code for two projects: a UART code to transmit temperature readings from an ADC to a screen, and an SPI code to transmit the same readings to a 7-segment display.
The UART code initializes the UART for 9600 baud transmission, enables the ADC at 1/64 clock, takes ADC readings every second, converts the reading to temperature, and transmits the temperature digits and a newline character to the screen.
The SPI code initializes the SPI interface in master mode, enables decoding on the 7-segment display for digits 1 and 2, takes ADC readings every second, converts the reading to temperature, and transmits the temperature digits to the 7-segment display using SPI commands.
The document contains code for two projects: a UART code to transmit temperature readings from an ADC to a screen, and an SPI code to transmit the same readings to a 7-segment display.
The UART code initializes the UART for 9600 baud transmission, enables the ADC at 1/64 clock, takes ADC readings every second, converts the reading to temperature, and transmits the temperature digits and a newline character to the screen.
The SPI code initializes the SPI interface in master mode, enables decoding on the 7-segment display for digits 1 and 2, takes ADC readings every second, converts the reading to temperature, and transmits the temperature digits to the 7-segment display using SPI commands.
The document contains code for two projects: a UART code to transmit temperature readings from an ADC to a screen, and an SPI code to transmit the same readings to a 7-segment display.
The UART code initializes the UART for 9600 baud transmission, enables the ADC at 1/64 clock, takes ADC readings every second, converts the reading to temperature, and transmits the temperature digits and a newline character to the screen.
The SPI code initializes the SPI interface in master mode, enables decoding on the 7-segment display for digits 1 and 2, takes ADC readings every second, converts the reading to temperature, and transmits the temperature digits to the 7-segment display using SPI commands.
double t = ADCL + (ADCH << 8);//get ADC value, convert to double t = (t *.48) +5; //convert ADC to temp unsigned int first = ((unsigned int)t/10) % 10;//get first digit of temp unsigned int second = ((unsigned int)t%10);//get second digit of temp while(! (UCSR0A & (1<<UDRE0))); UDR0 = first + 48; //send first char to screen
while(! (UCSR0A & (1<<UDRE0))); UDR0 = second + 48; //send second char to screen unsigned char str2[2] = " \n";//send linefeed to screen
SPDR = data; //send data while( !(SPSR & (1<<SPIF)));
PORTB |= (1<<SS); //terminate packet }
Set USART to transmit 8 char with 9600 baud rate. Turn on ADC with clock 1/64. Start ADC. Wait for ADC conversion to be done. NOT DONE DONE Delay 1 second. Transmit TEMP to screen. Convert ADC value to Fahrenheit. Convert Fahrenheit value to ASCII Send first Digit to screen. Send second Digit to screen. Send \n to screen.
int main(void)
{
ADCSRA = 0x86; //turn on ADC select clock 1/64 DDRB = (1<<MOSI)|(1<<SCK)|(1<<SS); //set MOSI, SCK, and SS to output SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR0); //enable SPI as master
execute( 0x09, 0b00000011); //enable decoding for digit 1 and 2 execute(0x0B, 0x02); //scan 2 segments execute(0x0C, 0x01); //turn on max7221
while(1) {
ADCSRA |= (1<<ADSC); //start conversion while((ADCSRA & (1<<ADIF)) == 0); //wait till conversion is done _delay_ms(1000); //delay 1 second
double t = ADCL + (ADCH << 8);//get ADC value, convert to double t = (t *.48) +5; //convert ADC to temp unsigned int first = ((unsigned int)t/10) % 10; //get first digit of temp unsigned int second = ((unsigned int)t%10); //get second digit of temp
execute(0x01, first ); //transmit first digit to seg 0 execute(0x02, second ); //transmit second digit to seg 1
} }
Code Flowchart:
Set MOSI SCK and SS to output. Enable SPI as master. Turn on ADC with clock 1/64. Start ADC. Wait for ADC conversion to be done. NOT DONE DONE Delay 1 second. Convert ADC value to Fahrenheit. Send first Digit to 7221. Send second Digit to 7221. Schematic