//****************************************************************************************** // PIC18F14K50 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H22.11.02 // Replace date : // History : // Laungage : MPLAB C18 //****************************************************************************************** // Description // // 1)Define I/O PORT // PORTC(0) : DCM16117A(E) // PORTC(1) : DCM16117A(RS) // PORTC(7-4) : DCM16117A(DB7-4) // // PORTC(2) : Terminal(0) // PORTB(4) : Terminal(1) // PORTB(6) : Terminal(2) // // PORTB(5) : Serial PORT(Rx) // PORTB(7) : Serial PORT(Tx) // // 2)OSC // 48MHz (12MHz Crystal * PLL4) // //****************************************************************************************** #include #include #include #include #include #include #include #include // //Define configration // #pragma config CPUDIV=NOCLKDIV //CPU System Clock Selection bit(No CPU System Clock divide) #pragma config USBDIV = OFF //USB Clock Selection bit(USB Clock comes directly from the OSC1/OSC2 oscillator block; no divide) #pragma config FOSC = HS //Oscillator Selection bits(HS oscillator) #pragma config PLLEN = ON //X PLL Enable bit(Oscillator multiplied by 4) #pragma config PCLKEN = OFF //Primary Clock Enable Bit(Primary clock is under software control) #pragma config FCMEN = OFF #pragma config IESO = OFF //Internal/External Oscillator Switchover bit(Oscillator Switchover mode disabled) #pragma config PWRTEN = OFF,BOREN = OFF,BORV = 19 #pragma config WDTEN = OFF,WDTPS = 1 #pragma config MCLRE = OFF //MCLR Pin Enable bit(RE3 input pin enabled; MCLR disabled) #pragma config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF #pragma config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF,WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF,EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF // //Define function // void Demo01(void); void Demo02(void); void Init_POS0(void); void Init_POS1(void); void Init_LCD(void); void LCD_Disp(unsigned char); void LCD_CLR(void); void LCD_SETH(unsigned char); void LCD_SETL(unsigned char); void BCF(unsigned int); void BSF(unsigned int); void mnop(unsigned int); void Wait_10mS(unsigned int); void Timer(void); void LCD_SCR_CLR(void); void LCD_SCR_DSP(unsigned char); void LCD_SCR_DSPS(unsigned char *, unsigned int); void M0puls(void); void M1puls(void); void M_hyp(unsigned int); void M_dot(unsigned int); void M_spe(unsigned int); void M_end(unsigned int); void M_out(char); void Mintim(void); // #define LCD_E 0 #define LCD_RS 1 #define LCD_DB4 4 #define LCD_DB5 5 #define LCD_DB6 6 #define LCD_DB7 7 #define TERM_0 2 #define TERM_1 4 #define TERM_2 6 #define PI_NORMAL 0 #define PI_FAILURE 1 // #define PI_LCD_bufsize 8 unsigned int G_LCD_IDX = 0; unsigned char G_LCD_BUFH[PI_LCD_bufsize]; unsigned char G_LCD_BUFL[PI_LCD_bufsize]; void main(void) { unsigned char Dat_str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; unsigned char tmp_char; unsigned int cnt = 0; // ***************************** // 1.Initialized Segment // ***************************** // (1)Define I/O port PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; TRISA = 0b11111111; TRISB = 0b11111111; TRISC = 0b00000000; ANSEL = 0b00000000; //All Degital PORT ANSELH = 0b00000000; //All Degital PORT // (2)Initialized LCD Init_LCD(); // ***************************** // 2.Program main // ***************************** // (1)Demo Program; Demo01(); while(1){ while((PORTB & 0b00010000)==0 || (PORTB & 0b01000000)==0); while((PORTB & 0b00010000)!=0 && (PORTB & 0b01000000)!=0){ cnt++; if(cnt>35)cnt=0; } tmp_char=Dat_str[cnt]; LCD_SCR_DSP(tmp_char); M_out(tmp_char); } } //****************************************************************************************** // Function name : Demo01 // Title : // Input : // Output : //****************************************************************************************** void Demo01(void) { // ******************* // Start Pic 18 表示 // ******************* // (1)領域0を選択 Init_POS0(); // (2)表示 LCD_Disp('S'); LCD_Disp('t'); LCD_Disp('a'); LCD_Disp('r'); LCD_Disp('t'); LCD_Disp(' '); LCD_Disp('P'); LCD_Disp('I'); // // (3)領域1を選択 Init_POS1(); // (4)表示 LCD_Disp('C'); LCD_Disp('1'); LCD_Disp('8'); LCD_Disp(' '); LCD_Disp(' '); LCD_Disp(' '); LCD_Disp(' '); LCD_Disp(' '); // (5)少し待つ Wait_10mS(100); // ******************* // 18F14K50 OK> 表示 // ******************* // (0)画面クリア LCD_CLR(); // (1)領域0を選択 Init_POS0(); // (2)表示 LCD_Disp('P'); LCD_Disp('I'); LCD_Disp('C'); LCD_Disp('1'); LCD_Disp('8'); LCD_Disp('F'); LCD_Disp('1'); LCD_Disp('4'); // // (3)領域1を選択 Init_POS1(); // (4)表示 LCD_Disp('K'); LCD_Disp('5'); LCD_Disp('0'); LCD_Disp(' '); LCD_Disp('O'); LCD_Disp('K'); LCD_Disp('>'); LCD_Disp(' '); // (5)少し待つ Wait_10mS(200); // ******************* // 画面クリア // ******************* LCD_CLR(); } //****************************************************************************************** // Function name : LCD_SCR_DSPS // Title : スクロール表示(文字列) // Input : // Output : //****************************************************************************************** void LCD_SCR_DSPS(unsigned char in_str[], unsigned int int_time) { unsigned int lp=0; while(1){ lp=0; while( in_str[lp] != 0x00){ LCD_SCR_DSP(in_str[lp]); Wait_10mS(int_time); lp++; } } } //****************************************************************************************** // Function name : LCD_SCR_CLR // Title : スクロール表示(クリア) // Input : // Output : //****************************************************************************************** void LCD_SCR_CLR(void) { G_LCD_IDX = 0; LCD_CLR(); Init_POS0(); } //****************************************************************************************** // Function name : LCD_SCR_DSP // Title : スクロール表示(キャラクタ) // Input : // Output : //****************************************************************************************** void LCD_SCR_DSP(unsigned char in_char) { unsigned int lp; if(G_LCD_IDX < PI_LCD_bufsize){ if(G_LCD_IDX == 0) Init_POS0(); LCD_Disp(in_char); G_LCD_BUFL[G_LCD_IDX] = in_char; G_LCD_IDX++; }else if(G_LCD_IDX < (PI_LCD_bufsize * 2)){ if(G_LCD_IDX == PI_LCD_bufsize) Init_POS1(); LCD_Disp(in_char); G_LCD_BUFH[(G_LCD_IDX - PI_LCD_bufsize)] = in_char; G_LCD_IDX++; }else{ for(lp=0;lp<( PI_LCD_bufsize - 1);lp++){ G_LCD_BUFL[lp] = G_LCD_BUFL[lp+1]; } G_LCD_BUFL[( PI_LCD_bufsize - 1)] = G_LCD_BUFH[0]; for(lp=0;lp<( PI_LCD_bufsize - 1);lp++){ G_LCD_BUFH[lp] = G_LCD_BUFH[lp+1]; } G_LCD_BUFH[( PI_LCD_bufsize - 1)] = in_char; Init_POS0(); for(lp=0;lp: Timer1 Input Clock Prescale Select bits // 11 = 1:8 Prescale value // 10 = 1:4 Prescale value // 01 = 1:2 Prescale value // 00 = 1:1 Prescale value //* T1CON(3) T1OSCEN: Timer1 Oscillator Enable bit // 1 = Timer1 oscillator is enabled // 0 = Timer1 oscillator is shut off // The oscillator inverter and feedback resistor are turned off to eliminate power drain. //* T1CON(2) T1SYNC: Timer1 External Clock Input Synchronization Select bit // When TMR1CS = 1: // 1 = Do not synchronize external clock input // 0 = Synchronize external clock input // When TMR1CS = 0: // This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0. //* T1CON(1) TMR1CS: Timer1 Clock Source Select bit // 1 = External clock from the T13CKI pin (on the rising edge) // 0 = Internal clock (FOSC/4) //* T1CON(0) bit 0 TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // ******************************************************* T1CON = 0b10110001; // 16-bit Read/Write Mode, 1:8 Prescale value // ******************************************************* // 2.Define TMR1 Register // Caution(16-bit Read/Write Mode) !!!!: // (a)Writing to TMR1H does not directly affect Timer1. // Instead, the high byte of Timer1 is updated with the contents of TMR1H when a write occurs to TMR1L. // This allows all 16 bits of Timer1 to be updated at once. // (b)A read from TMR1L will load the contents of the high byte of Timer1 into the Timer1 high byte buffer. // This provides the user with the ability to accurately read all 16 bits of Timer1 without the need to // determine whether a read of the high byte, followed by a read of the low byte, has become invalid due // to a rollover or carry between reads. // ******************************************************* // TMR1 data ( OSC:12MHz, 4X PLLON, 1:8 Prescale value, 0.01[S];100Hz ) // 10mS(100Hz): 12MHz * 4(PLLON) / 4(PIC) / 8(Prescale) / 100(100Hz)==> 0d15,000(Count) ==> 0x3A98(Count) // TMR1 = 0xFFFF - 0x3A98 ==> 0xC567 // TMR1H = 0xC5; TMR1L = 0x67; // ******************************************************* // 3.Timer // ******************************************************* PIR1bits.TMR1IF = 0; while(1){ if(PIR1bits.TMR1IF != 0 )break; } } //****************************************************************************************** // Function Name : M_out // Title : // Input : // Output : // Description : //****************************************************************************************** void M_out(char in_char ) { char m_data[15]; unsigned int lp; unsigned int Speed=40; // ******************** // 1.Set data // ******************** if(in_char==' ') strcpypgm2ram(m_data," "); if(in_char=='A') strcpypgm2ram(m_data,"0-"); if(in_char=='B') strcpypgm2ram(m_data,"-000"); if(in_char=='C') strcpypgm2ram(m_data,"-0-0"); if(in_char=='D') strcpypgm2ram(m_data,"-00"); if(in_char=='E') strcpypgm2ram(m_data,"0"); if(in_char=='F') strcpypgm2ram(m_data,"00-0"); if(in_char=='G') strcpypgm2ram(m_data,"--0"); if(in_char=='H') strcpypgm2ram(m_data,"0000"); if(in_char=='I') strcpypgm2ram(m_data,"00"); if(in_char=='J') strcpypgm2ram(m_data,"0---"); if(in_char=='K') strcpypgm2ram(m_data,"-0-"); if(in_char=='L') strcpypgm2ram(m_data,"0-00"); if(in_char=='M') strcpypgm2ram(m_data,"--"); if(in_char=='N') strcpypgm2ram(m_data,"-0"); if(in_char=='O') strcpypgm2ram(m_data,"---"); if(in_char=='P') strcpypgm2ram(m_data,"0--0"); if(in_char=='Q') strcpypgm2ram(m_data,"--0-"); if(in_char=='R') strcpypgm2ram(m_data,"0-0"); if(in_char=='S') strcpypgm2ram(m_data,"000"); if(in_char=='T') strcpypgm2ram(m_data,"-"); if(in_char=='U') strcpypgm2ram(m_data,"00-"); if(in_char=='V') strcpypgm2ram(m_data,"000-"); if(in_char=='W') strcpypgm2ram(m_data,"0--"); if(in_char=='X') strcpypgm2ram(m_data,"-00-"); if(in_char=='Y') strcpypgm2ram(m_data,"-0--"); if(in_char=='Z') strcpypgm2ram(m_data,"--00"); if(in_char=='0') strcpypgm2ram(m_data,"-----"); if(in_char=='1') strcpypgm2ram(m_data,"0----"); if(in_char=='2') strcpypgm2ram(m_data,"00---"); if(in_char=='3') strcpypgm2ram(m_data,"000--"); if(in_char=='4') strcpypgm2ram(m_data,"0000-"); if(in_char=='5') strcpypgm2ram(m_data,"00000"); if(in_char=='6') strcpypgm2ram(m_data,"-0000"); if(in_char=='7') strcpypgm2ram(m_data,"--000"); if(in_char=='8') strcpypgm2ram(m_data,"---00"); if(in_char=='9') strcpypgm2ram(m_data,"----0"); // ******************** // 2.Output // ******************** for(lp=0;lp<15;lp++){ switch(m_data[lp]){ case '0': M_dot(Speed); M_spe(Speed); break; case '-': M_hyp(Speed); M_spe(Speed); break; case ' ': M_end(Speed); break; default: M_end(Speed); return; } } } //****************************************************************************************** // Function Name : M_hyp // Title : // Input : // Output : // Description : //****************************************************************************************** void M_hyp(unsigned int len ) { unsigned int lp; len=len*3; for(lp=0;lp: Timer1 Input Clock Prescale Select bits // 11 = 1:8 Prescale value // 10 = 1:4 Prescale value // 01 = 1:2 Prescale value // 00 = 1:1 Prescale value //* T1CON(3) T1OSCEN: Timer1 Oscillator Enable bit // 1 = Timer1 oscillator is enabled // 0 = Timer1 oscillator is shut off // The oscillator inverter and feedback resistor are turned off to eliminate power drain. //* T1CON(2) T1SYNC: Timer1 External Clock Input Synchronization Select bit // When TMR1CS = 1: // 1 = Do not synchronize external clock input // 0 = Synchronize external clock input // When TMR1CS = 0: // This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0. //* T1CON(1) TMR1CS: Timer1 Clock Source Select bit // 1 = External clock from the T13CKI pin (on the rising edge) // 0 = Internal clock (FOSC/4) //* T1CON(0) bit 0 TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // ******************************************************* T1CON = 0b10000001; // 16-bit Read/Write Mode, 1:8 Prescale value // ******************************************************* // 2.Define TMR1 Register // Caution(16-bit Read/Write Mode) !!!!: // (a)Writing to TMR1H does not directly affect Timer1. // Instead, the high byte of Timer1 is updated with the contents of TMR1H when a write occurs to TMR1L. // This allows all 16 bits of Timer1 to be updated at once. // (b)A read from TMR1L will load the contents of the high byte of Timer1 into the Timer1 high byte buffer. // This provides the user with the ability to accurately read all 16 bits of Timer1 without the need to // determine whether a read of the high byte, followed by a read of the low byte, has become invalid due // to a rollover or carry between reads. // ******************************************************* // TMR1 data ( OSC:12MHz, 4X PLLON, 1:8 Prescale value, 0.01[S];100Hz ) // 10mS(100Hz): 12MHz * 4(PLLON) / 4(PIC) / 8(Prescale) / 100(100Hz)==> 0d15,000(Count) ==> 0x3A98(Count) // TMR1 = 0xFFFF - 0x3A98 ==> 0xC567 // 2mS(500Hz): 12MHz * 4(PLLON) / 4(PIC) / 1(Prescale) / 500(500Hz)==> 0d24,000(Count) ==> 0x5DC0(Count) // TMR1 = 0xFFFF - 0x5DC0 ==> 0xA23F // 1mS(1000Hz): 12MHz * 4(PLLON) / 4(PIC) / 1(Prescale) / 1000(1000Hz)==> 0d12,000(Count) ==> 0x2EE0(Count) // TMR1 = 0xFFFF - 0x2EE0 ==> 0xD11F // TMR1H = 0xA2; TMR1L = 0x3F; // ******************************************************* // 3.Timer // ******************************************************* PIR1bits.TMR1IF = 0; while(1){ if(PIR1bits.TMR1IF != 0 )break; } }