#ifndef F_CPU #define F_CPU 16000000UL // telling controller crystal frequency (16 MHz AVR ATMega328P) #endif // SPI INTERFACE DEFINES #define MOSI 3 // MOSI it's PORT B, PIN 3 #define MISO 4 // MISO it's PORT B, PIN 4 #define SCK 5 // SCK it's PORT B, PIN 5 #define SS 2 // SS it's PORT B, PIN 2 // RESET THE DISPLAY #define RST 0 // RESET it's PORT B, PIN 0 //DISPLAY MODE SELECT - Input to select either command/address or data input. #define DC 1 // DC it's PORT B, PIN 1 //array of distance levels [0..40] unsigned char Dist_value[8] = {0,0,0,0,0,0,0,0}; // array of distance level signs [1..8] static const unsigned char LVL_DIST[8][8] = { { 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80}, // 1 { 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0}, // 2 { 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0}, // 3 { 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0}, // 4 { 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8}, // 5 { 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC}, // 6 { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE}, // 7 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, // 8 }; // array of digits [0..9] and sign > static const unsigned char DIG[11][8] = { {0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00, 0x00, 0x00} // 0 ,{0x00, 0x42, 0x7F, 0x40, 0x00, 0x00, 0x00, 0x00} // 1 ,{0x42, 0x61, 0x51, 0x49, 0x46, 0x00, 0x00, 0x00} // 2 ,{0x21, 0x41, 0x45, 0x4B, 0x31, 0x00, 0x00, 0x00} // 3 ,{0x18, 0x14, 0x12, 0x7F, 0x10, 0x00, 0x00, 0x00} // 4 ,{0x27, 0x45, 0x45, 0x45, 0x39, 0x00, 0x00, 0x00} // 5 ,{0x3C, 0x4A, 0x49, 0x49, 0x30, 0x00, 0x00, 0x00} // 6 ,{0x01, 0x71, 0x09, 0x05, 0x03, 0x00, 0x00, 0x00} // 7 ,{0x36, 0x49, 0x49, 0x49, 0x36, 0x00, 0x00, 0x00} // 8 ,{0x06, 0x49, 0x49, 0x29, 0x1E, 0x00, 0x00, 0x00} // 9 ,{0x00, 0x41, 0x22, 0x14, 0x08, 0x00, 0x00, 0x00} // > }; // codes array of words "MIN_MAX:" static const unsigned char MIN_MAX[25] = { 0x0E, 0x01, 0x0E, 0x01, 0x0E, 0x00, 0x0E, 0x05, 0x0E, 0x00, 0x0D, 0x02, 0x0D, 0xE0, 0x10, 0xE0, 0x10, 0xE0, 0x00, 0xF0, 0x00, 0xF0, 0x20, 0x40, 0xF0 }; // codes array of word "DIST:" const unsigned char DIST[15] = { 0x3E, 0x22, 0x1C, 0x00, 0x3A, 0x00, 0x24, 0x2A, 0x12, 0x00, 0x02, 0x3E, 0x02, 0x00, 0x14 }; // codes array of word "CM:" const unsigned char CM[9] = { 0x1C, 0x22, 0x22, 0x00, 0x3C, 0x02, 0x3C, 0x02, 0x3C }; //pulse length volatile unsigned short hc_sr04_cnt; #include #include #include #include "uart.h" // Port Initialization void Port_Init() { DDRB = (1< 0) { Display_SetXY(15+i*8,5-lvl); for (int index1=0; index1<8; index1++) { SPDR = LVL_DIST[Dist_value[i]-lvl * 8][index1]; // Load codes array data into the buffer of display while(!(SPSR & (1< 0) // if result is quarter-digit number { Display_Dig(temp_int_0,42,0); // display 1 digit of light level Display_Dig(temp_int_1,48,0); // display 2 digit of light level } else { if(temp_int_1 > 0) // if result is triple-digit number { Off_Dig(42,0); // clear 1 sign of number Display_Dig(temp_int_1,48,0); // display 1 digit of light level*/ } } } } // Function to display words void Display_label() { // Word "DIST" Display_SetXY(20,0); // Set the address of position on display (up row) for (int index=0; index<15; index++) { SPDR = DIST[index]; // Load codes array data into the buffer of display while(!(SPSR & (1<0; i--) Dist_value[i] = Dist_value[i-1]; Dist_value[0] = value; } int main(void) { unsigned char sm; Port_Init(); // Port Initialization SPI_Init(); // SPI Initialization SPI_SS_Enable(); // Display Enable Display_init(); // Display initialization Display_Clear();// Display Clear Display_label(); // Display Interface UART_Init(9600); /* Initialize UART at 9600 baud rate */ sei(); // Infinity loop while (1) { sm = hc_sr04_meas(); UART_Printf("TCNT1:%5d\n\r",TCNT1); // Send the value on UART UART_Printf("SM:%5d\n\r",sm); // Send the value on UART if (sm > 40) {sm=40;Display_Dist(41);} else {Display_Dist(sm);} Add_Dist_value(sm); Display_level(); _delay_ms( 200 ); } }