void LCD1602_Init() { LCD1602_EN_L; LCD1602_BL_H; Delay15ms();//绛夊緟15ms锛岀瓑寰匧CD鐢垫簮绋冲畾 LCD1602_WriteCom(0x03); // - - 璁剧疆4浣嶆牸寮忥紝2琛岋紝5x7 Delay5ms(); LCD1602_WriteCom(0x03); // - - 璁剧疆4浣嶆牸寮忥紝2琛岋紝5x7 Delay200us(); LCD1602_WriteCom(0x03); // - - 璁剧疆4浣嶆牸寮忥紝2琛岋紝5x7 LCD1602_WaitBusy(); LCD1602_WriteCom(0x02); // - - 璁剧疆4浣嶆牸寮忥紝2琛岋紝5x7 LCD1602_WaitBusy(); LCD1602_WriteCom(0x28); // - - 璁剧疆4浣嶆牸寮忥紝2琛岋紝5x7 LCD1602_WaitBusy(); LCD1602_WriteCom(0x0c); // - - 鏁翠綋鏄剧ず锛屽叧鍏夋爣锛屼笉闂儊 LCD1602_WaitBusy(); LCD1602_WriteCom(0x06); // - - 璁惧畾杈撳叆鏂瑰紡锛屽閲忎笉绉讳綅 LCD1602_WaitBusy(); LCD1602_WriteCom(0x01); // - - 娓呴櫎灞忓箷鏄剧ず }
void LCDChar(unsigned char c) { LCD_RS = 1; LCD_DATA &= 0xf0; LCD_DATA |= (c >> 4); PulseEnable; LCD_DATA &= 0xf0; LCD_DATA |= (c & 0x0f); PulseEnable; Delay5ms(); }
void SetPWMFreq(short freq) { short prescale=6103; uint8_t oldmode,newmode; freq*=0.9; prescale/=freq; oldmode =IIC_Read(PCA_Add,PCA9685_MODE1); newmode =(oldmode&0x7f)|0x10; IIC_Write(PCA_Add,PCA9685_MODE1,newmode); IIC_Write(PCA_Add,PCA9685_PRESCALE,prescale); IIC_Write(PCA_Add,PCA9685_MODE1,oldmode); Delay5ms(); IIC_Write(PCA_Add,PCA9685_MODE1,oldmode|0xa1); }
//WriteSPI_to_LCD() Function: //WriteSPI_to_LCD() writes 32 characters to the 2x16 LCD //using the SPI1 interface in a polling fashion. //After each byte is written, the Interrupt Flag bit is polled and the //next character is written after the interrupt flag bit is set. void WriteSPI_to_LCD(void) { int temp, i; i=0; temp = SPI1BUF; SPI1STATbits.SPIROV = 0; IFS0bits.SPI1IF = 0; Delay5us(50); SPI1BUF = LCDLINE1CMD; //First write the command to set cursor //to Line1 on LCD LCDCharPtr = &DisplayData[0]; //Set up LCD character pointer to point //to the Display buffer while(i < 16) { while (IFS0bits.SPI1IF=0); //Now write 16 characters temp = SPI1BUF; //to Line 1 of the LCD IFS0bits.SPI1IF = 0; SPI1STATbits.SPIROV = 0; Delay5ms(1); SPI1BUF = *LCDCharPtr++; i++; } while (IFS0bits.SPI1IF==0); temp = SPI1BUF; IFS0bits.SPI1IF = 0; SPI1STATbits.SPIROV = 0; temp = *LCDCharPtr++; //Some characters in the Display buffer temp = *LCDCharPtr++; //are skipped while writing to LCD. CR Delay5us(50); //and LF are for writing to RS2322 UART SPI1BUF = LCDLINE2CMD; //Next, write the command to set cursor //to Line2 on LCD i = 0; while(i < 16) { while (IFS0bits.SPI1IF==0); //Now write 16 characters temp = SPI1BUF; //to Line 2 of the LCD IFS0bits.SPI1IF = 0; SPI1STATbits.SPIROV = 0; Delay5us(50); SPI1BUF = *LCDCharPtr++; i++; } }
int main(void) { // Declaration and initialization of message-related data unsigned int txMsgSID = 0; unsigned char txMsgData[12] = {'1', '2', '3', '4', '5', 'S', 'X', 'U', 'C', 'L', 'D', 'R'}; unsigned char buffer[32]; unsigned int lenght = 0; unsigned int cursorPosition = 0; // Initialize keyboard, CAN and LCD KeybInit(); CANinit(LOOPBACK_MODE, 0x0000, 0x0000); LCDInit(); // Clear LCD LCDClear(); // Char to save key pressed unsigned char c; while(1) { if(CANtransmissionCompleted()) { // Get key pressed c = getKeyBlocking(); if(lenght < 32 && cursorPosition < 32) { buffer[lenght] = txMsgData[c]; lenght++; if(c == 0 || c == 1 || c == 2 || c == 3 || c == 4 || c == 11) { switch(c) { case 0: LCDPrint("1"); break; case 1: LCDPrint("2"); break; case 2: LCDPrint("3"); break; case 3: LCDPrint("4"); break; case 4: LCDPrint("5"); break; case 11: LCDMoveRight(); break; } // Cursor managment - Update position if(cursorPosition == 15) { // Cursor is at the end of the first line // Move to second line LCDMoveSecondLine(); } // Increase cursor position cursorPosition++; } else if(c == 6 || c == 9) { // Pressed erase and go back key ( <- ) if(cursorPosition == 0) { // Cursor is at the begining of the first line // Do nothing } else if(cursorPosition == 16) { // Cursor is at the begining of the second line // Move cursor to the last position of the first line LCDSetCursor(15); if(c == 6) { // Erase content (print a white space) LCDPrint(" "); // Move cursor back and decrease cursor position LCDMoveLeft(); } cursorPosition--; } else { // Cursor is between extreme positions or at position 32 // Move cursor back LCDMoveLeft(); if(c == 6) { // Erase content (print a white space) LCDPrint(" "); // Move cursor back and decrease cursor position LCDMoveLeft(); } cursorPosition--; } } else if(c == 7) { // Move to first line LCDMoveFirstLine(); cursorPosition = 0; } else if(c == 10) { // Move to second line LCDMoveSecondLine(); cursorPosition = 16; } else { // Pressed clear (C) or send (S) if(c == 8) { // Clear LCD LCDClear(); // Move to home LCDMoveHome(); // Set counter of cursor position to 0 (zero) cursorPosition = 0; } else { // Clear LCD and show message //////////////////// // SENDING! // // // //////////////////// LCDClear(); LCDMoveHome(); LCDPrint(" SENDING!"); // Wait 1s before start send for(i = 0; i < 200; i++) Delay5ms(); // Clear LCD LCDClear(); // Send message according keys pressed for(i = 0; i < lenght; i++) { CANsendMessage(txMsgSID, buffer[i]); // Wait until transmission complete while(CANtransmissionCompleted() == 0); // Wait 0,5s before send the next for(j = 0; j < 100; j++) Delay5ms(); } lenght = 0; cursorPosition = 0; LCDMoveHome(); } } } else { // Clear LCD and show message in two lines //////////////////// // WARNING! // // Full memory // //////////////////// LCDClear(); LCDMoveHome(); LCDPrint(" WARNING!"); LCDMoveSecondLine(); LCDPrint(" Full memory"); // Pressed key send (S) if(c == 5) { // Clear LCD and show message //////////////////// // SENDING! // // // //////////////////// LCDClear(); LCDMoveHome(); LCDPrint(" SENDING!"); // Wait 1s before start send for(i = 0; i < 200; i++) Delay5ms(); // Clear LCD LCDClear(); // Send message according keys pressed for(i = 0; i < lenght; i++) { CANsendMessage(txMsgSID, buffer[i]); // Wait until transmission complete while(CANtransmissionCompleted() == 0); // Wait 0,5s before send the next for(j = 0; j < 100; j++) Delay5ms(); } lenght = 0; cursorPosition = 0; LCDMoveHome(); } } } // Wait 0,25s before to detect another time the key pressed for(i = 0; i < 50; i++) Delay5ms(); } }