Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C code for PIC16F1508 with ADC and PWM modules

Status
Not open for further replies.

Flyback

Well-Known Member
Hi,
Please could you possibly check this XC8 C code for PIC16F1508?
All it is supposed to do is read the ADC input, and if greater than 10, then get on send PWMs on PWM1 and PWM3
(PWM T = 1ms and 50% duty cycle)
It wont build as for some reason it doesnt like the "T2CKPS" nemonic

Code:
//pic16f1508  LED COLOR mixer

// CONFIG

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config STVREN = OFF     // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)


// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdint.h>

#define  _XTAL_FREQ 4000000

    uint16_t   count;
    uint16_t   count1;
    uint16_t   count2;
    uint16_t   vbusmeas;

void main(void) {
    //SETUP PORTS

    TRISA = 0x38;   //ips are temp, commsbus
    TRISB = 0x00;
    TRISC = 0x04;
    
    //RA2 = PWM into RC for dimming
    //RC1 = PWM DIMMING OR TURN OFF
    
   /* PORT FUNCTIONS
    RA0 = DAT
    RA1 = CLK
    RA2 = DIM1_UC...dim the HV9910B with PWM into RC....................
    RA3 = MCLR
    RA4 = TEMP input    (MAKE OP LOW FOR NOW)
    RA5 = COMMSBUS  (MAKE IP) ***
    RA6 = NO PIN
    RA7 = NO PIN
            
    RB0 = NO PIN
    RB1 = NO PIN
    RB2 = NO PIN
    RB3 = NO PIN
    RB4 = NC OP LOW
    RB5 = NC OP LOW
    RB6 = NC OP LOW
    RB7 = NC OP LOW
            
    RC0 = CHILICOMMS OP LOW   (MAKE OP LOW FOR NOW) ..NNEDS WEAK PULLUP
    RC1 = TURN OFF HV9910B (OP)
    RC2 = VBUSMEAS
    RC3 = NC OP LOW
    RC4 = COMMSBUSDRIVE OP LOW
    RC5 = COLORMIX OP LOW..................................................
    RC6 = SC_OUT OP LOW   (MAKE OP LOW FOR NOW)
    RC7 = CURRENT MONITOR (MAKE OP LOW FOR NOW)     */
            
            
    //INITIATE PORTS
        LATAbits.LATA0 = 0;
        LATAbits.LATA1 = 0; //
        LATAbits.LATA2 = 0; //
        //LATAbits.LATA3 = 0;   MCLR
        //LATAbits.LATA4 = 0;     //temp sensor input
        //LATAbits.LATA5 = 1;     
        //LATAbits.LATA6 = 0;     //NO INTERNAL PIN
        //LATAbits.LATA7 = 0;     //NO INTERNAL PIN
                  
        //LATBbits.LATB0 = 0;   //NO INTERNAL PIN 
        //LATBbits.LATB1 = 0;   //NO INTERNAL PIN
        //LATBbits.LATB2 = 0;   //NO INTERNAL PIN     
        //LATBbits.LATB3 = 0;   //NO INTERNAL PIN   
        LATBbits.LATB4 = 0;
        LATBbits.LATB5 = 0;           
        LATBbits.LATB6 = 0;
        LATBbits.LATB7 = 0;
                
        LATCbits.LATC0 = 0;
        LATCbits.LATC1 = 1;     //TURN OFF LED DRIVER   
        //LATCbits.LATC2 = 0;     
        LATCbits.LATC3 = 0;     
        LATCbits.LATC4 = 0;
        LATCbits.LATC5 = 1;             
        LATCbits.LATC6 = 0;
        LATCbits.LATC7 = 0;
        
        //SETUP FUNCTION REGISTERS
        ANSELA = 0x00;
        ANSELB - 0x00;
        ANSELC = 0x04;   //VBUSMEAS
        
        ADCON0 = 0x18;  //choose AN6
        ADCON1 = 0x40;  // fosc/4, leftjust, refvdd
        ADRESH = 0x00;
    
        CM1CON0 = 0x00;
        CM1CON1 = 0x00;
        CM2CON0 = 0x00;
        CM2CON1 = 0x00;
        
        INTCON = 0x00;
        
        OPTION_REG = 0x07;  //wpu BY latch
        WPUA = 0x10;    //temperature input
        WPUB = 0x00;
        //WPUC = 0x81;    //CURRNT MO, Chilicomms //NO WPUC
    
        T2CON = 0x00;
      
        ADON = 1;    //ENABLE ADC
        
        PWM1CON = 0x00;
        PWM3CON = 0x00;
    
        
        LATAbits.LATA2 = 0; //zero light
        LATCbits.LATC1 = 1; //led driver off
        
        
        
        while(1) {
        GO = 1;    //START CONVERSION
        while(GO) {;}
        vbusmeas = ADRESH;
        if (vbusmeas > 10) {break;}
        }
        
        //SET UP PWM1 (RC5) FOR 50% DUTY CYCLE AT T= 1ms
        //Page 253 of PIC16F1508 DATASHEET
        TRISC5 = 1;
        PWM1CON = 0x00;
        PR2 = 0xFF;
        PWM1DCH = 0x00;
        PWM1DCL = 0x00;
        TMR2IF = 0;
        T2CKPS = 0x00;
        TMR2ON = 1;
        PWM1EN = 1;
        while (!TMR2IF){;}
        TRISC5 = 0;
        PWM1OE = 1;
        PWM1EN = 1;
        
        
        //SET UP PWM3 (RA2) FOR 50% DUTY CYCLE AT T= 1ms
        //Page 253 of PIC16F1508 DATASHEET
        TRISA2 = 1;
        PWM3CON = 0x00;
        PR2 = 0xFF;
        PWM3DCH = 0x00;
        PWM3DCL = 0x00;
        TMR2IF = 0;
        T2CKPS = 0x00;
        TMR2ON = 1;
        PWM3EN = 1;
        while (!TMR2IF){;}
        TRISC5 = 0;
        PWM3OE = 1;
        PWM3EN = 1;
        
        
        while(1){;}
 
    return;
}
 
Try changing it to T2CON &= 0xfc; - looks like an error in the definition file.

Also, why have you got it twice?

Mike.
 
Thanks i am using V1.44 XC8 C.

Also, the modus operandi to set up the PWM module (datasheet pg 253) doesnt seem to make sense...it first tells you to set the relevant TRIS bit, it then tells you to clear it.

PIC16F1508 datasheet

Setting the TRIS bit allows you to setup the PWM before it actually activates. It is for more complicated motor controls (H-bridge) where you want to completely set up before any mosfet get energized. Bad things like shoot-through can happen if you start sending out one of the various PWM (half bridge, full bridge, signal ) that is possible with some microchip products.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top