//****************************************************************************************** // PIC18F452 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H23.07.10 // Replace date : // History : // Laungage : MPLAB C18 //****************************************************************************************** // Description // // PORTB(0) : DCM16117A(RS) // PORTB(1) : DCM16117A(E) // PORTB(7-4) : DCM16117A(DB7-4) // // PORTA(0)AN0 : KXM52(Output X) // PORTA(1)AN1 : KXM52(Output Y) // PORTA(2)AN2 : KXM52(Output Z) // // PORTD(0-7) : Terminal2(0-7) // // PORTC(7) : Serial PORT(Rx) // PORTC(6) : Serial PORT(Tx) // // // 2)OSC // 40MHz (10MHz Crystal * PLL4) // //****************************************************************************************** #include #include #include #include #include #include #include #include // //Define configration // #pragma config OSC=HSPLL //Oscillator Selection : HS-PLL Enabled #pragma config OSCS=OFF //Osc. Switch Enable: #pragma config PWRT=OFF //Power-up Timer: #pragma config BOR=OFF //Brown-out Reset: #pragma config BORV=25 //Brown-out Voltage: 2.5V #pragma config WDT=OFF //Watchdog Timer: #pragma config WDTPS=1 //Watchdog Postscaler: 1:1 #pragma config CCP2MUX=OFF //CCP2 MUX: Disable(RB3) //#pragma config CCP2MUX = ON //CCP2 MUX: Enable(RC1) #pragma config STVR=OFF //Stack Overflow Reset: #pragma config LVP=OFF //Low Voltage ICSP: #pragma config DEBUG=OFF //Background Debugger Enable: #pragma config CP0=OFF //Code Protection Block 0: #pragma config CP1=OFF //Code Protection Block 1: #pragma config CP2=OFF //Code Protection Block 2: #pragma config CP3=OFF //Code Protection Block 3: #pragma config CPB=OFF //Boot Block Code Protection: #pragma config CPD=OFF //Data EEPROM Code Protection: #pragma config WRT0=OFF //Write Protection Block 0: #pragma config WRT1=OFF //Write Protection Block 1: #pragma config WRT2=OFF //Write Protection Block 2: #pragma config WRT3=OFF //Write Protection Block 3: #pragma config WRTB=OFF //Boot Block Write Protection: #pragma config WRTC=OFF //Configuration Register Write Protection: #pragma config WRTD=OFF //Data EEPROM Write Protection: #pragma config EBTR0=OFF //Table Read Protection Block 0: #pragma config EBTR1=OFF //Table Read Protection Block 1: #pragma config EBTR2=OFF //Table Read Protection Block 2: #pragma config EBTR3=OFF //Table Read Protection Block 3: #pragma config EBTRB=OFF //Boot Block Table Read Protection: // //Define function // #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 0 #define TERM_1 1 #define TERM_2 2 #define PI_NORMAL 0 #define PI_FAILURE 1 #define PI_POS0 0 #define PI_POS1 1 #define PI_POSN 2 #define PI_AN0 0 #define PI_AN1 1 #define PI_AN2 2 #define PI_1G 250 // // //Define function // void Demo01(void); void Init_POS0(void); void Init_POS1(void); void Init_LCD(void); void LCD_Disp_S_AUT(unsigned char *); void LCD_Disp_S(unsigned char *, unsigned char); 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 Timer(void); void Wait_1mS(unsigned int); void Init_USART(void); void SendUART_s(char *); void Trans_USART(unsigned char ); void Rec_USART(unsigned char * ); void Init_ADC(char); void GetADC(unsigned int *, char); void Prog01(void); void Prog02(void); void PRD_N(unsigned char *, unsigned char *); void main(void) { // ************************************* // 1.Initialize Segment // ************************************* // (1)TRIS TRISA=0b11111111; TRISB=0b00001100; TRISC=0b11111111; TRISD=0b11111111; TRISE=0b00000111; //Caution !!! (Bit[7-4]) // (2)PORT PORTA=0b00000000; PORTB=0b00000000; PORTC=0b00000000; PORTD=0b00000000; PORTE=0b00000000; // (3)LAT LATA=0b00000000; LATB=0b00000000; LATC=0b00000000; LATD=0b00000000; LATE=0b00000000; // (4)Select A/D Converter ADCON1=0b00000111; //ALL Degital Port // (5)Initialized LCD Init_LCD(); // ************************************* // 2.Program Main // ************************************* // (1)Demo Message Demo01(); // (2)Program main while(1){ Prog02(); } } //****************************************************************************************** // Function name : Demo01 // Title : // Input : // Output : //****************************************************************************************** void Demo01(void) { unsigned char msg[17]; // ******************* // 1.Start Pic18f452 表示 // ******************* strcpypgm2ram(msg," Start PIC18F452"); LCD_Disp_S_AUT(msg); Wait_1mS(1000); // ******************* // 2.18F14f452 OK> 表示 // ******************* LCD_CLR(); // 画面クリア // strcpypgm2ram(msg,"PIC18F452 OK> "); LCD_Disp_S_AUT(msg); Wait_1mS(1000); // LCD_CLR(); // 画面クリア } //****************************************************************************************** // Function name : Prog01 // Title : X,Y,Z (Volt[mV]) // Input : // Output : //****************************************************************************************** void Prog01(void){ unsigned int x_dat,y_dat,z_dat; unsigned long Lx_dat,Ly_dat,Lz_dat; unsigned char tmp_line[9]; unsigned char tmp_line_T[17]; // ************************ // 1.Data X // ************************ Init_ADC(PI_AN0); GetADC(&x_dat,PI_AN0); Lx_dat = x_dat; Lx_dat = Lx_dat * (long)5000 / (long)1023; sprintf(tmp_line_T,"%4ld",Lx_dat); // ************************ // 2.Data Y // ************************ Init_ADC(PI_AN1); GetADC(&y_dat,PI_AN1); Ly_dat = y_dat; Ly_dat = Ly_dat * (long)5000 / (long)1023; sprintf(tmp_line,"%4ld",Ly_dat); strcpypgm2ram(&tmp_line_T[4]," "); strcpy(&tmp_line_T[6],tmp_line); // ************************ // 3.Data Z // ************************ Init_ADC(PI_AN2); GetADC(&z_dat,PI_AN2); Lz_dat = z_dat; Lz_dat = Lz_dat * (long)5000 / (long)1023; sprintf(tmp_line,"%4ld",Lz_dat); strcpypgm2ram(&tmp_line_T[10]," "); strcpy(&tmp_line_T[12],tmp_line); // ************************ // 4.Put data // ************************ LCD_Disp_S_AUT(tmp_line_T); } //****************************************************************************************** // Function name : Prog02 // Title : X,Y,Z (G) // Input : // Output : //****************************************************************************************** void Prog02(void){ unsigned int x_dat,y_dat,z_dat; unsigned long Lx_dat,Ly_dat,Lz_dat; unsigned char tmp_line[9]; unsigned char tmp_line_1[9]; unsigned char tmp_line_T[17]; // ************************ // 1.Data X // ************************ Init_ADC(PI_AN0); GetADC(&x_dat,PI_AN0); Lx_dat = x_dat; Lx_dat = Lx_dat * (long)5000 / (long)1023 ; Lx_dat = Lx_dat / 10 - (long)PI_1G ; sprintf(tmp_line,"%4ld",Lx_dat); PRD_N(tmp_line_T, tmp_line); // ************************ // 2.Data Y // ************************ Init_ADC(PI_AN1); GetADC(&y_dat,PI_AN1); Ly_dat = y_dat; Ly_dat = Ly_dat * (long)5000 / (long)1023 ; Ly_dat = Ly_dat / 10 - (long)PI_1G; sprintf(tmp_line,"%4ld",Ly_dat); PRD_N(tmp_line_1, tmp_line); strcpy(&tmp_line_T[5],tmp_line_1); // ************************ // 3.Data Z // ************************ Init_ADC(PI_AN2); GetADC(&z_dat,PI_AN2); Lz_dat = z_dat; Lz_dat = Lz_dat * (long)5000 / (long)1023 ; Lz_dat = Lz_dat / 10 - (long)PI_1G; sprintf(tmp_line,"%4ld",Lz_dat); PRD_N(tmp_line_1, tmp_line); strcpy(&tmp_line_T[10],tmp_line_1); // ************************ // 4.Put data // ************************ LCD_Disp_S_AUT(tmp_line_T); } //****************************************************************************************** // Function name : PRD_N // Title : // Input : // Output : //****************************************************************************************** void PRD_N(unsigned char out_s[], unsigned char in_s[]) { unsigned int lp,len; // // 1.Check Error // len = strlen(in_s); if(len != 4){ strcpypgm2ram(out_s," *.**"); return; } // // 2.Check Sign // out_s[0] = ' '; for(lp=0;lp16) len=16; if(len<1){ }else if(len<9){ 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 = 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:10MHz, 4X PLLON, 1:1 Prescale value, 0.001[S];1000Hz ) // 1mS(1000Hz): 10MHz * 4(PLLON) / 4(PIC) / 1(Prescale) / 1000(1000Hz)==> 0d10,000(Count) ==> 0x2710(Count) // TMR1 = 0xFFFF - 0x2710 ==> 0xD8EF // TMR1H = 0xD8; TMR1L = 0xEF; // ******************************************************* // 3.Timer // ******************************************************* PIR1bits.TMR1IF = 0; while(1){ if(PIR1bits.TMR1IF != 0 )break; } } //**************************************************************************** // Function Name : Init_USART // Title : // Input : // Output : // Description : //**************************************************************************** void Init_USART(void) { // ******************************************************* // 1.Define TXSTA(TRANSMIT STATUS AND CONTROL) Register // TXSTA(7) CSRC: Clock Source Select bit // Asynchronous mode:Don’t care // TXSTA(6) TX9: 9-bit Transmit Enable bit // 1 = Selects 9-bit transmission // 0 = Selects 8-bit transmission // TXSTA(5) TXEN: Transmit Enable bit(1) // 1 = Transmit enabled // 0 = Transmit disabled // TXSTA(4) SYNC: EUSART Mode Select bit // 1 = Synchronous mode // 0 = Asynchronous mode // TXSTA(3) Unimplemented: Read as '0' // TXSTA(2) BRGH: High Baud Rate Select bit // Asynchronous mode: // 1 = High speed // 0 = Low speed // TXSTA(1) TRMT: Transmit Shift Register Status bit // 1 = TSR empty // 0 = TSR full // TXSTA(0) TX9D: Ninth bit of Transmit Data // Can be address/data bit or a parity bit. // ******************************************************* // TXSTA = 0b00100100; TXSTA = 0b00100000; // ******************************************************* // 2.Define RCSTA(RECEIVE STATUS AND CONTROL) Register // RCSTA(7) SPEN: Serial Port Enable bit // 1 = Serial port enabled (configures RX/DT and TX/CK pins as serial port pins) // 0 = Serial port disabled (held in Reset) // RCSTA(6) RX9: 9-bit Receive Enable bit // 1 = Selects 9-bit reception // 0 = Selects 8-bit reception // RCSTA(5) SREN: Single Receive Enable bit // Asynchronous mode: // Don’t care // RCSTA(4) CREN: Continuous Receive Enable bit // Asynchronous mode: // 1 = Enables receiver // 0 = Disables receiver // RCSTA(3) ADDEN: Address Detect Enable bit // Asynchronous mode 9-bit (RX9 = 1): // 1 = Enables address detection, enable interrupt and load the receive buffer when RSR<8> is set // 0 = Disables address detection, all bytes are received and ninth bit can be used as parity bit // Asynchronous mode 8-bit (RX9 = 0): // Don’t care // RCSTA(2) FERR: Framing Error bit // 1 = Framing error (can be updated by reading RCREG register and receive next valid byte) // 0 = No framing error // RCSTA(1) OERR: Overrun Error bit // 1 = Overrun error (can be cleared by clearing bit CREN) // 0 = No overrun error // RCSTA(0) RX9D: Ninth bit of Received Data // This can be address/data bit or a parity bit and must be calculated by user firmware. // ******************************************************* RCSTA=0b10010000; // ******************************************************* // 3.Define SPBRG(Baud Rate Generator Register) Register // ******************************************************* // 9600BPS, OSC:40MHz(40MHz*PLL) ==> SPREG=64 SPBRG=64; } //****************************************************************************************** // Function Name : SendUART_s // Title : // Input : // Output : // Description : Write TXREG //****************************************************************************************** void SendUART_s(char *put_string) { while(*put_string!=0x00){ Trans_USART(*put_string); put_string++; } } //**************************************************************************** // Function Name : Trans_USART // Title : // Input : // Output : // Description : //**************************************************************************** void Trans_USART(unsigned char IO_data ) { char chk_flg; // // 1.Check Send Flag // while(1){ chk_flg = TXSTA & 0x02; //0000,0010 TXSTA(TRMT) if(chk_flg != 0x00)break; } // // 2.Set data // TXREG = IO_data; } //**************************************************************************** // Function Name : Rec_USART // Title : // Input : // Output : // Description : //**************************************************************************** void Rec_USART(unsigned char *IO_data ) { char chk_flg; // // 1.Check Recvive Flag // while(1){ chk_flg = PIR1 & 0x20; //0010,0000 PIR1(RCIF) if(chk_flg != 0x00)break; } // // 2.Check error // chk_flg = RCSTA & 0x06; //0000,0110 RCSTA(FERR,OERR) See PIC16F88 Manual P.98 if(chk_flg != 0x00){ *IO_data = 0x00; return; } // // 3.Set Recive Data // *IO_data = RCREG; }