void rtc_test() { rtc_t rtc; UART_Printf("\n\rConnections SCL->PC.3 SDA->PC.4"); UART_Printf("\n\r Make connections and hit 'k' to test! "); while(UART_RxChar()!='k'); RTC_Init(); rtc.hour = 0x10; // 10:40:20 am rtc.min = 0x40; rtc.sec = 0x00; rtc.date = 0x01; //1st Jan 2016 rtc.month = 0x01; rtc.year = 0x16; rtc.weekDay = 5; // Friday: 5th day of week considering monday as first day. /*##### Set the time and Date only once. Once the Time and Date is set, comment these lines and reflash the code. Else the time will be set every time the controller is reset*/ RTC_SetDateTime(&rtc); // 10:40:20 am, 1st Jan 2016 /* Display the Time and Date continuously */ while(1) { RTC_GetDateTime(&rtc); UART_Printf("\n\rtime:%2x:%2x:%2x \nDate:%2x/%2x/%2x",(uint16_t)rtc.hour,(uint16_t)rtc.min,(uint16_t)rtc.sec,(uint16_t)rtc.date,(uint16_t)rtc.month,(uint16_t)rtc.year); } }
void rtc_test() { unsigned char sec,min,hour,day,month,year; UART_Printf("\n\rConnections SCL->P0.6 SDA->P0.7"); UART_Printf("\n\r Make connections and hit 'k' to test! "); while(UART_RxChar()!='k'); RTC_Init(); /*##### Set the time and Date only once. Once the Time and Date is set, comment these lines and reflash the code. Else the time will be set every time the controller is reset*/ RTC_SetTime(0x10,0x40,0x00); // 10:40:20 am RTC_SetDate(0x01,0x01,0x15); // 1st Jan 2015 /* Display the Time and Date continuously */ while(1) { RTC_GetTime(&hour,&min,&sec); RTC_GetDate(&day,&month,&year); UART_Printf("\n\rtime:%2x:%2x:%2x \nDate:%2x/%2x/%2x",(uint16_t)hour,(uint16_t)min,(uint16_t)sec,(uint16_t)day,(uint16_t)month,(uint16_t)year); } }
void seg_test() { unsigned char seg_code[]={0xC0,0xF9,0xA4,0xB0}; UART_TxString("\n\r Segment DataBus: PORTD Seg select: S1->PB.0 S2->PB.1 S3->PB.2 S4->PB.3"); UART_Printf("\n\rMake connections and hit 'k' to test! "); while(UART_RxChar()!='k'); SegValueDirnReg = C_PortOutput_U8; SegSelectDirnReg = C_PortOutput_U8; while(1) { SegmentSlection=SegOne; SegmentValue = seg_code[0]; DELAY_us(10); SegmentSlection=SegTwo; SegmentValue = seg_code[1]; DELAY_us(10); SegmentSlection=SegThree; SegmentValue = seg_code[2]; DELAY_us(10); SegmentSlection=SegFour; SegmentValue = seg_code[3]; DELAY_us(10); } }
void LCD_test() { UART_TxString("\n\r LCD DataBus: P2 Control: RS-P0.0 RW-P0.1 E-P0.2 "); UART_Printf("\n\r Make connections and hit 'k' to test "); while(UART_RxChar()!='k'); LCD_Init(8,2,16); DELAY_ms(100); LCD_DisplayString("Explore Embedded!"); while(1); }
void gpio_test() { unsigned char i; UART_Printf("\n\rConnect any IO Pins to buzzer, relays, leds "); UART_Printf("\n\rMake connections and hit 'k' to test "); while(UART_RxChar()!='k'); ADCON0 = 0x00; ADCON1 = 0x07; TRISA = C_PortOutput_U8; TRISB = C_PortOutput_U8; TRISC = C_PortOutput_U8; TRISD = C_PortOutput_U8; TRISE = C_PortOutput_U8; while(1) { for(i=0;i<2;i++) { /* Turn On all the leds and wait for one second */ PORTA = 0xff; PORTB = 0xff; PORTC = 0xff; PORTD = 0xff; PORTE = 0xff; DELAY_ms(500); /* Turn off all the leds and wait for one second */ PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; DELAY_ms(500); } for(i=1;i;i=i<<1) { PORTA = i; PORTB = i; PORTC = i; PORTD = i; PORTE = i; DELAY_ms(100); } for(i=0x80;i;i=i>>1) { PORTA = i; PORTB = i; PORTC = i; PORTD = i; PORTE = i; DELAY_ms(100); } } }
void LCD_4bit_test() { UART_TxString("\n\r LCD DataBus:(PD4-PD7) RS-PB.0 RW-PB.1 EN-PB.2 "); UART_Printf("\n\r Make connections and hit 'k' to test "); while(UART_RxChar()!='k'); LCD_SetUp(PB_0,PB_1,PB_2,P_NC,P_NC,P_NC,P_NC,PD_4,PD_5,PD_6,PD_7); LCD_Init(2,16); DELAY_ms(100); LCD_DisplayString("Explore Embedded"); LCD_DisplayString("Lcd 4-bit Mode"); while(1); }
/***************************************************KEYPAD ***************************************** Displays keys pressed on 4x4 keypad on the uart *****************************************************TEST*******************************************/ void keypad_test() { uint8_t key; UART_Printf("\n\rConnections: PORTD"); UART_Printf("\n\rMake connections and hit 'k' to test!"); while(UART_RxChar()!='k'); KEYPAD_Init(PD_0,PD_1,PD_2,PD_3,PD_4,PD_5,PD_6,PD_7); while(1) { key = KEYPAD_GetKey(); UART_Printf("\n\r Key:%c", key); } }
/***************************************************KEYPAD ***************************************** Displays keys pressed on 4x4 keypad on the uart *****************************************************TEST*******************************************/ void keypad_test() { uint8_t key; UART_Printf("\n\rConnections: Port 2"); UART_Printf("\n\rMake connections and hit 'k' to test!"); while(UART_RxChar()!='k'); KEYPAD_Init(); while(1) { key = KEYPAD_GetKey(); UART_Printf("\n\r Key:%c", key); } }
/***************************************************EEPROM***************************************** Writes and Reads a character to and from EEPROM *****************************************************TEST*****************************************/ void eeprom_test() { unsigned char eeprom_address=0x00, write_char = 'X', read_char; UART_Printf("Connections SCL->P0.6 SDA->P0.7"); UART_Printf("Make connections and hit 'k' to test! "); while(UART_RxChar()!='k'); UART_TxString("\n\rEeprom Write: "); //Print the message on UART UART_TxChar(write_char); //Print the char to be written EEPROM_WriteByte(eeprom_address,write_char); // Write the data at memoryLocation 0x00 UART_TxString(" Eeprom Read: "); //Print the message on UART read_char = EEPROM_ReadByte(eeprom_address); // Read the data from memoryLocation 0x00 UART_TxChar(read_char); }
void gpio_test() { UART_Printf("\n\rConnect any IO Pins to buzzer, relays, leds "); UART_Printf("\n\rMake connections and hit 'k' to test "); while(UART_RxChar()!='k'); while(1) { /* Turn On all the leds and wait for one second */ P0= P1 = P2 =P3= 0xff; DELAY_sec(1); /* Turn off all the leds and wait for one second */ P0= P1 = P2 =P3= 0x00;; DELAY_sec(1); } }
/* start the main program */ void main() { char ch; /* Initilize the UART before Transmitting/Receiving any data */ UART_Init(); while(1) { /*Receive and Transmit a char indefinitely at 9600 baud rate */ ch=UART_RxChar(); // Receive a char and store it in "ch" UART_TxChar(ch); //Transmit the received char } }
/***************************************************ADC ***************************************** Reads and displays ADC data from on board sensors. *****************************************************TEST*****************************************/ void adc_test() { uint16_t temp,light,pot; UART_Printf("\n\rConnections: DataBus P1 Control: adc_A->P0.0 adc_B->P0.1 adc_C->P0.2 adc_ALE->P.3 adc_Start->P.4 adc_EOC->P1.5 adc_OE->P1.6"); UART_Printf("\n\rMake connections and hit 'k' to test!"); while(UART_RxChar()!='k'); ADC_Init(); while(1) { temp = ADC_GetAdcValue(0); pot = ADC_GetAdcValue(1); light = ADC_GetAdcValue(2); UART_Printf("\n\rtemp:%3d pot:%3d light:%3d", temp,pot,light); } }
void UART_RxString(uint8_t var_uartChannel_u8, char *ptr_stringPointer_u8) { char ch; while(1) { ch=UART_RxChar(var_uartChannel_u8); //Receive a char UART_TxChar(var_uartChannel_u8,ch); //Echo back the received char if((ch=='\r') || (ch=='\n')) //read till enter key is pressed { //once enter key is pressed null terminate the string *ptr_stringPointer_u8=0; //and break the loop break; } *ptr_stringPointer_u8=ch; //copy the char into string and increment the pointer ptr_stringPointer_u8++; } }
/*--------------------------------------------------------------------------------- void UART_RxString(char *string_ptr) ---------------------------------------------------------------------------------- * I/P Arguments: *string_ptr Address of the string where the received data needs to be stored * Return value : none * description : 1.This function is used to receive a ASCII string through UART till the carriage_return/New_line 2.The string_ptr points to the begining of the string and each time UART_RxChar() function is called to receive a char and copy it into the buffer(STRING) and incrment string_ptr. 3.Once the carriage_return/New_line is encountered the loop is breaked and the String is NULL terminated. *****NOTE*******: 1.The received char is ECHOED back, if not required then comment UART_TxChar(ch) in the code. 2.BackSlash is not taken care. ___________________________________________________________________________________*/ void UART_RxString(char *string_ptr) { char ch; while(1) { ch=UART_RxChar(); //Reaceive a char UART_TxChar(ch); //Echo back the received char if((ch=='\r') || (ch=='\n')) //read till enter key is pressed { //once enter key is pressed *string_ptr=0; //null terminate the string break; //and break the loop } *string_ptr=ch; //copy the char into string. string_ptr++; //and increment the pointer } }
/* start the main program */ void main() { UART_Init(9600); UART_TxString("\n\rTest menu Utra x51 v1.1\r\n 1:GPIO Blink\r\n 2:LCD \n\r 3:7-Segment\n\r 4:RTC\n\r 5:EEPROM\n\r 6:ADC\n\r 7:Keypad \n\r Enter option:"); UART_TxString("\n\rReset the board after test is done"); mm_option = UART_RxChar(); while(1) { switch(mm_option) { case '1': gpio_test(); break; case '2': LCD_test(); break; case '3': seg_test(); break; case '4': rtc_test(); break; case '5': eeprom_test(); break; //eeprom case '6': adc_test(); break; case '7': keypad_test();break; default:break; } } }
/* start the main program */ void main() { UART_Init(9600); UART_TxString("\n\rPIC Ultra baord menu v2.0\r\n 1:GPIO Blink\r\n 2:LCD 8-bit \n\r 3:LCD 4-bit\n\r 4:7-Segment\n\r 5:RTC\n\r 6:EEPROM\n\r 7:ADC\n\r 8:Keypad \n\r Enter option:"); UART_TxString("\n\rReset the board after test is done"); mm_option = UART_RxChar(); while(1) { switch(mm_option) { case '1': gpio_test(); break; case '2': LCD_8bit_test(); break; case '3': LCD_4bit_test(); break; case '4': seg_test(); break; case '5': rtc_test(); break; case '6': eeprom_test(); break; //eeprom case '7': adc_test(); break; case '8': keypad_test();break; default:break; } } }
/* start the main program */ void main() { char ch; /* Initilize the UART before Transmitting/Receiving any data */ UART_Init(); /* Initilize the lcd before displaying any thing on the lcd */ LCD_Init(); while(1) { /*Receive and Transmit a char indefinitely at 9600 baud rate */ ch=UART_RxChar(); // Receive a char and store it in "ch" UART_TxChar(ch); //Transmit the received char LCD_DataWrite(ch); //Display the received char on LCD } }
/* Run one iteration of the interface state machine. This is responsible for displaying the command * prompt, receiving characters, and running the resulting command by searching for and calling the * proper command function. Call this in your main loop. */ void Cmd_ProcessInterface() { switch(m_cmdstate) { case eCmd_Prompt: if(UART_TxAvailable() >= 4) { UART_TxData("\r#> ", 4); m_cmdlen = 0; ++m_cmdstate; } break; case eCmd_Receive: { uint16_t avail = UART_RxAvailable(); while(avail--) { char c = UART_RxChar(); wdt_reset(); if(0x08 == c) // backspace -- remove last character { if(m_cmdlen > 0) --m_cmdlen; } else if(0x1B == c) // escape -- clear prompt { m_cmdstate = eCmd_Prompt; break; } else // add the character to the buffer { m_cmdbuf[m_cmdlen] = c; ++m_cmdlen; // either got a command or buffer is full if(c == '\r' || m_cmdlen >= CMD_BUFSIZE) { m_cmdbuf[m_cmdlen] = 0; // terminate command ++m_cmdstate; break; } } } wdt_reset(); } break; case eCmd_Run: m_cmdstate = eCmd_Prompt; if(m_numcmds > 0) { uint8_t i; // look for the right command and call its function for(i = 0; i < m_numcmds; ++i) { size_t len = strlen(m_cmds[i].name); if(0 == strncmp(m_cmds[i].name, m_cmdbuf, len) && (m_cmdbuf[len] == ' ' || m_cmdbuf[len] == '\r')) { m_cmds[i].cmdfunc(m_cmdbuf, m_cmdlen); break; } } // didn't find the command if(i == m_numcmds) UART_TxData_P(PSTR("Unknown command\r"), 16); } break; case eCmd_Help: if(m_helpindex < m_numcmds) { size_t txlen = (strlen(m_cmds[m_helpindex].name) + strlen_P(m_cmds[m_helpindex].help) + 5); if(txlen > UART_TX_BUFSIZE) txlen = UART_TX_BUFSIZE; if(UART_TxAvailable() >= txlen) { UART_TxString(m_cmds[m_helpindex].name); UART_TxData("\t - ", 4); UART_TxString_P(m_cmds[m_helpindex].help); UART_TxChar('\r'); ++m_helpindex; } } else { m_cmdstate = eCmd_Prompt; m_helpindex = 0; } break; default: m_cmdstate = eCmd_Prompt; break; } }