//********************************************************************** /*   「LCDモニター(3.3V駆動)」  ■通信速度   ・9600bps  → SW1=1、SW2=1   ・19200bps → SW1=0、SW2=1   ・38400bps → SW1=1、SW2=0   ・4800bps  → SW1=0、SW2=0  ■動作モード   ・受信モード → SW3=1     ※受信したデータを16文字2行の32の表示エリアに      順次表示していきます。     ※データが、CRまたはLFの時には、画面をクリアせずに、カーソル      を1行目の1文字目に戻します。     ※ データが、FFの時には、画面をクリアし、 カーソルを1行目の      1文字目に戻します。   ・送信モード → SW3=1     ※テストデータの送信を繰り返します。      :英字(大文字)“A”~“Z”      :英字(小文字)“a”~“z”      :数字     “0”~“9” */ //********************************************************************** #define SW1 PORTA.F3 #define SW2 PORTA.F4 #define SW3 PORTA.F5 #define CR 0x0D #define LF 0x0A #define FF 0x0C #define LED PORTA.F2 #define ON 1 #define OFF 0 //********************************************************************** void Pwm_Change_DutyEx(unsigned int duty_ratio) { CCPR1L = duty_ratio >> 2; CCP1CON.CCP1Y = duty_ratio & 0b00000001; CCP1CON.CCP1X = (duty_ratio & 0b00000010) >> 1; } //********************************************************************** void recv() { static unsigned short rd, cnt; // cnt = 0; // while (1) { LED = OFF; if (Usart_Data_Ready() == 0) continue; LED = ON; rd = Usart_Read(); // if ((rd == CR) || (rd == LF)) { Lcd_Custom_Cmd(LCD_FIRST_ROW); cnt = 0; continue; } if (rd == FF) { Lcd_Custom_Cmd(LCD_FIRST_ROW); Lcd_Custom_Cmd(LCD_CLEAR); cnt = 0; continue; } // switch (cnt) { case 0: Lcd_Custom_Cmd(LCD_FIRST_ROW); Lcd_Custom_Chr_Cp(rd); break; case 16: Lcd_Custom_Cmd(LCD_SECOND_ROW); Lcd_Custom_Chr_Cp(rd); break; default: Lcd_Custom_Chr_Cp(rd); break; } // if (cnt < 31) { cnt++; } else { cnt = 0; } } } //********************************************************************** void send() { static unsigned short cnt; // while (1) { //'A'.....'Z' LED = ON; Usart_Write(FF); Soft_Uart_Write(FF); for (cnt = 0; cnt < 26; cnt++) { Usart_Write(cnt + 'A'); Soft_Uart_Write(cnt + 'A'); Delay_ms(10); } LED = OFF; Delay_ms(1000); //'a'.....'z' LED = ON; Usart_Write(FF); Soft_Uart_Write(FF); for (cnt = 0; cnt < 26; cnt++) { Usart_Write(cnt + 'a'); Soft_Uart_Write(cnt + 'a'); Delay_ms(10); } LED = OFF; Delay_ms(1000); //'0'.....'9' LED = ON; Usart_Write(FF); Soft_Uart_Write(FF); for (cnt = 0; cnt < 10; cnt++) { Usart_Write(cnt + '0'); Soft_Uart_Write(cnt + '0'); Delay_ms(10); } LED = OFF; Delay_ms(1000); } } //********************************************************************** void main() { OSCCON = 0b01110000; CMCON = 0b00000111; ANSEL = 0b00000000; TRISA = 0b00111000; TRISB = 0b00000100; //LCDのマイナス電源用 Pwm_Init(10000); //10kHz Pwm_Change_DutyEx((PR2 * 4) / 2); //50% Pwm_Start(); // Lcd_Custom_Config(&PORTA, 1, 0, 7, 6, &PORTB, 4, 6, 7); // Lcd_Custom_Cmd(LCD_CURSOR_OFF); Lcd_Custom_Cmd(LCD_CLEAR); LED = ON; Lcd_Custom_Out(1, 1, "Lcd Monitor v3"); if ((SW1 == 1) && (SW2 == 1)) { Lcd_Custom_Out(2, 9, " 9600bps"); } if ((SW1 == 0) && (SW2 == 1)) { Lcd_Custom_Out(2, 9, "19200bps"); } if ((SW1 == 1) && (SW2 == 0)) { Lcd_Custom_Out(2, 9, "38400bps"); } if ((SW1 == 0) && (SW2 == 0)) { Lcd_Custom_Out(2, 9, " 4800bps"); } // if ((SW1 == 1) && (SW2 == 1)) { Usart_Init(9600); Soft_Uart_Init(PORTB, 0, 1, 9600, 0); } if ((SW1 == 0) && (SW2 == 1)) { Usart_Init(19200); Soft_Uart_Init(PORTB, 0, 1, 19200, 0); } if ((SW1 == 1) && (SW2 == 0)) { Usart_Init(38400); Soft_Uart_Init(PORTB, 0, 1, 38400, 0); } if ((SW1 == 0) && (SW2 == 0)) { Usart_Init(4800); Soft_Uart_Init(PORTB, 0, 1, 4800, 0); } Delay_ms(500); Lcd_Custom_Cmd(LCD_CLEAR); LED = OFF; // if (SW3 == 1) { recv(); } else { send(); } } //**********************************************************************