//****************************************************************************************** // PIC18F14K50 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H22.01.10 // History : // Laungage : MPLAB C18 //****************************************************************************************** // (1)Description // //****************************************************************************************** #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 Timer(void); void Wait(unsigned int); void IntDAC(void); void SetDAC(unsigned char); //****************************************************************************************** // Program : // Title : // Input : // Output : //****************************************************************************************** void main(void) { static unsigned char G_sin_data[] = {100,131,159,181,195,200,195,181,159,131, 100, 69, 41, 19, 5, 0, 5, 19, 41, 69 }; unsigned char Web_data[20]; unsigned char lp; // ***************************** // 1.Initialized Segment // ***************************** // (1)Define I/O Port TRISA = 0xFF; TRISB = 0xFF; TRISC = 0xFF; // (2)Define Voltage Reference IntDAC(); // ***************************** // 2.Program main // ***************************** // (1)Initialized Web data for(lp=0;lp<20;lp++) Web_data[lp] = G_sin_data[lp] /@7 ; // 0 <= Web_data <= 31(0x1f) // (2)Output Web data while(1){ for(lp=0;lp<20;lp++){ SetDAC(Web_data[lp]); Timer(); // 0.001[Second] } }; } //****************************************************************************************** // Function Name : IntDAC // Title : Initialized Voltage Reference // Input : // Output : //****************************************************************************************** void IntDAC(void) { // ******************************************************* // 0.Define PORTC Register // ******************************************************* TRISC = TRISC | 0b00000100; // ******************************************************* // 1.Define REFCON0(REFERENCE CONTROL REGISTER 0) // REFCON0(7) FVR1EN: Fixed Voltage Reference 1 Enable bit // 0 = FVR is disabled // 1 = FVR is enabled !!! FVR ; 1.024V Fixed Reference Voltage // REFCON0(6) FVR1ST: Fixed Voltage Reference 1 Stable bit // 0 = FVR is not stable // 1 = FVR is stable // REFCON0(5-4) FVR1S<1:0>: Fixed Voltage Reference 1 Voltage Select bits // 00 = Reserved, do not use // 01 = 1.024V (x1) // 10 = 2.048V (x2) // 11 = 4.096V (x4) // REFCON0(bit 3-0) // Unimplemented: Read as e0f // ******************************************************* REFCON0 = 0b11110000; // ******************************************************* // 2.Define REFCON1(REFERENCE CONTROL REGISTER 1) // REFCON1(7) D1EN: DAC 1 Enable bit // 0 = DAC 1 is disabled // 1 = DAC 1 is enabled // REFCON1(6) D1LPS: DAC 1 Low-Power Voltage State Select bit // 0 = VDAC = DAC1 Negative reference source selected // 1 = VDAC = DAC1 Positive reference source selected // REFCON1(5) DAC1OE: DAC 1 Voltage Output Enable bit // 1 = DAC 1 voltage level is also outputed on the RC2/AN6/P1D/C12IN2-/CVREF/INT2 pin // 0 = DAC 1 voltage level is disconnected from RC2/AN6/P1D/C12IN2-/CVREF/INT2 pin // REFCON1(4) Unimplemented: Read as e0f // REFCON1(3-2) D1PSS<1:0>: DAC 1 Positive Source Select bits // 00 = VDD // 01 = VREF+ // 10 = FVR output // 11 = Reserved, do not use // REFCON1(1) Unimplemented: Read as e0f // REFCON1(0) D1NSS: DAC1 Negative Source Select bits // 0 = VSS // 1 = VREF // ******************************************************* REFCON1 = 0b11101000; // ******************************************************* // 2.Define REFCON2(REFERENCE CONTROL REGISTER 2) // REFCON2(7-5) Unimplemented: Read as e0f // REFCON2(4-0) DAC1R<4:0>: DAC1 Voltage Output Select bits // VOUT = ((VSOURCE+) - (VSOURCE-))*(DAC1R<4:0>/(2^5)) + VSOURCE // ******************************************************* REFCON2 = 0b00000000; } //****************************************************************************************** // Function Name : SetDAC // Title : Set Voltage Reference // Input : 0 <= in_data <= 31(0x1f) // Output : //****************************************************************************************** void SetDAC(unsigned char in_data) { // ******************************************************* // 1.Define REFCON2(REFERENCE CONTROL REGISTER 2) // REFCON2(7-5) Unimplemented: Read as e0f // REFCON2(4-0) DAC1R<4:0>: DAC1 Voltage Output Select bits // VOUT = ((VSOURCE+) - (VSOURCE-))*(DAC1R<4:0>/(2^5)) + VSOURCE // ******************************************************* REFCON2 = in_data; } //****************************************************************************************** // Function Name : Wait // Title : wait function // Input : timer(1mS * timer) // Output : //****************************************************************************************** void Wait(unsigned int timer) { unsigned int lp; 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 // 10mS(100Hz): 12MHz * 4(PLLON) / 4(PIC) / 1(Prescale) / 1000(1000Hz)==> 0d12,000(Count) ==> 0x2EE0(Count) // TMR1 = 0xFFFF - 0x2EE0 ==> 0xD11F // TMR1H = 0xD1; TMR1L = 0x1F; // ******************************************************* // 3.Timer // ******************************************************* PIR1bits.TMR1IF = 0; while(1){ if(PIR1bits.TMR1IF != 0 )break; } }