12f683 to 16f690

Discussion in 'Programmer's Corner' started by 7Arrows, Feb 23, 2010.

  1. 7Arrows

    Thread Starter New Member

    Feb 14, 2010
    11
    0
    Hello

    This program was written using MPLAB IDE and the HiTech C compiler for a 12f683 I’m trying to convert it to run in a 16f690. I’ve missed something changing over the code it will build and upload but the timers won’t function. When the program runs the pulse string works but on delay control. I underlined the lines of code that have been changed.

    Thank you.

    Tinker2

    //--- Includes ---
    #include <htc.h>
    __CONFIG(INTIO & WDTDIS & PWRTDIS & MCLREN & UNPROTECT & BORDIS & IESODIS & FCMDIS);

    //--- Defines ---
    #define INPUT_PIN 1
    #define OUTPUT_PIN 0

    #define TxPulse GPIO1 Change to RC0
    #define Sample1 GPIO5 Change to RC1
    #define Sample2 GPIO4 Change to RC2
    #define PWM GPIO2 Change to RA1
    #define ADC GPIO0 Change to RA2

    #define TrisTxPulse TRISIO1 Change to TRISC0
    #define TrisSample1 TRISIO5 Change to TRISC1
    #define TrisSample2 TRISIO4 Change to TRISC2
    #define TrisPWM TRISIO2 Change to TRISA1
    #define TrisADC TRISIO0 Change to TRISA2


    //--- Variables ---
    unsigned int Period = 255-125; // 1000 Hz (PS=16)
    unsigned int TxPulseWidth = 65535-100; // 100us
    unsigned int Sample1Delay = 65535-15; // 15us
    unsigned int Sample2Delay = 65535-200; // 200us
    unsigned int SampleWidth = 65535-15; // 15us

    //--- Prototypes ---
    void Init(void);
    void InitAdc(void);
    void InitTimer0(void);
    void InitTimer1(void);
    void Timer1Delay(unsigned int);
    void Process(void);


    // Main
    //
    void main(void)
    {
    // First, set up the internal oscillator
    IRCF2=1; IRCF1=1; IRCF0=1; // Internal osc. freq. = 8MHz
    OSTS = 0; // Use the internal osc
    SCS = 1; // Use the internal osc
    while(!HTS); // Wait for clock to stabilise

    // Next, initialize everything
    Init(); // below
    InitAdc();
    InitTimer1(); // Do this BEFORE InitTimer0()
    InitTimer0();

    // Now start the interrupt timer and idle
    TMR0 = Period; // Set the timer
    T0IF = 0; // Clear Timer0 flag
    while(1) {} // Keep the processor active
    }

    // Initialization stuff

    // Initialize the I/O pins and set some flags
    //
    void Init(void)
    {
    TrisTxPulse = OUTPUT_PIN; // Set TX pulse to output
    TrisSample1 = OUTPUT_PIN; // Set main sample to output
    TrisSample2 = OUTPUT_PIN; // Set alt sample to output
    TrisPWM = INPUT_PIN; // Set pwm to input (not used in this version)
    TrisADC = INPUT_PIN; // Set ADC to input (not used in this version)

    GIE = 1; // Master interrupt enable
    PEIE = 0; // Peripheral interrupt disable
    CMCON0 = 0x07; Change to CM1CON0 = 0x70; // Turn off analog comparator

    TxPulse = 1; // Initialize starting pulse values
    Sample1 = 1;
    Sample2 = 1;
    }

    // Initialize the ADC. With the register cleared the default sample rate is Fosc/2
    // which is the max speed.
    //
    void InitAdc(void)
    {
    ANSEL = 0; // Clear the ADC select register
    ADCON0 = 0; // Clear the ADC control register
    VCFG = 0; // Use VDD as Vref
    ADCS2=0; ADCS1=0; ADCS0=1; // ADC clock = fosc/8
    ADFM = 0; // Left(MSB)-justified data
    ANS0 = 1; // AN0 (pin 7) is set up as an ADC input
    CHS1=0; CHS0=0; // AN0 is the selected ADC input
    ADON = 0; // Turn the ADC off (not used in this version)
    }

    // Timer0 is 8 bits with Div16, so delay is (255-n)*16*4/Fosc
    // For Fosc = 8MHz Timer0 has 8us of resolution
    //
    void InitTimer0(void)
    {
    T0IE = 1; // Enable Timer0 interrupt
    T0CS = 0; // Clock off Fosc/4
    PSA = 0; // Prescaler assigned to Timer0
    PS2 = 0; PS1 = 1; PS0 = 1; // Prescaler = /16 (8us resolution)
    }

    // Timer1 is 16 bits and clocked at Fosc/4; use PS=2 for 1us resolution
    //
    void InitTimer1(void)
    {
    TMR1CS = 0; // Use Fosc/4
    T1CKPS1 = 0; T1CKPS0 = 1; // Prescaler = /2 (1us resolution)
    TMR1H = 0; TMR1L = 0; // Clear the registers
    T1GE = 0;
    T1OSCEN = 0;
    TMR1IE = 0; // Don't allow an interrupt
    TMR1ON = 1; // Turn Timer1 on
    TMR1IF = 0; // Clear the flag
    }


    // Processing routines

    // Delay (in us) = 65535-n
    // Because of the fudge factor, 15us is the minimum delay.
    //
    void Timer1Delay(unsigned int n)
    {
    n += 15; // Adjust for fixed error (approx 15us)
    TMR1H = n >> 8; // Load the upper counter
    TMR1L = n; // Load the lower counter
    TMR1IF = 0; // Clear the flag
    while(TMR1IF == 0) {;} // Wait for a flag
    }

    // ISR() is the interrupt service routine for Timer0. The main loop is
    // triggered off this interrupt and must be completed before the next
    // interrupt.
    //
    static void interrupt ISR(void)
    {
    static int i;

    T0IF = 0; // Clear the Timer0 Int flag
    TMR0 = Period+2; // Reset the timer (+2 fudge factor)

    TxPulse = 0;
    Timer1Delay(TxPulseWidth); // Transmit pulse
    TxPulse = 1;

    Timer1Delay(Sample1Delay); // Main sample delay
    Sample1 = 0;
    Timer1Delay(SampleWidth); // Main sample pulse
    Sample1 = 1;

    Timer1Delay(Sample2Delay); // Alt sample delay
    Sample2 = 0;
    Timer1Delay(SampleWidth); // Alt sample pulse
    Sample2 = 1;

    Process();
    }
     
Loading...