Quantcast
Channel: MEL PICBASIC Forum
Viewing all articles
Browse latest Browse all 4745

10-Bit PWM using PIC16F1509

$
0
0
Hi All,

I am working my way through the process of setting up a 10-Bit PWM channel using a PIC16F1509 and have hit a major hurdle - I am stumped and don't know what to do.
My project is for an RGB LED Controller using slider pots to control the PWM signal being fed to the RGB LEDs.
I have the basic principle operating using HPWM commands but this limits the control to 8-bits and I want to take advantage of the device's 10-bit PWM capability.
Using the calculation in the datasheet, I can achieve 10-bit PWM resolution for my desired 250Hz PWM frequency with a 4MHz internal oscillator, PR2 = 255 and TMR2 Prescale = 16
My problem is when I try to set the PWM1CON register or load the high and low PWM duty cycle registers.
My code is as follows:

Code:

'****************************************************************
'*  Name    : RGB_LED_Controller_16F1509.pbp                    *
'*  Author  : Barry Phillips                                    *
'*  Notice  : Copyright (c) 2015 Baztronics                    *
'*          : All Rights Reserved                              *
'*  Date    : 27/02/2015                                        *
'*  Version : 1.0                                              *
'*  Notes  : RGB LED Controller using PIC16F1509              *
'*          :                                                  *
'****************************************************************
#CONFIG
    __config _CONFIG1,    _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
    __config _CONFIG2,    _WRT_OFF & _STVREN_OFF & _LPBOR_OFF & _LVP_OFF
  #ENDCONFIG
 
OSCCON= %01101000                  'Internal 4MHz Oscillator
DEFINE OSC 4                      'Internal RC Oscillator 8MHz

' Connect LCD D4 to RB4 (Pin 13)
' Connect LCD D5 to RB5 (Pin 12)
' Connect LCD D6 to RB6 (Pin 11)
' Connect LCD D7 to RB7 (Pin 10)
' Connect LCD R/S to RC3 (Pin 7)
' Connect LCD E  to RC6 (Pin 8)
' Connect POT1 to RA0 (Pin 19)
' Connect POT2 to RA1 (Pin 18)
' Connect POT3 to RC0 (Pin 16)
' Connect POT4 to RC2 (Pin 14)
' Connect PWM1 to RC5 (Pin 5)
' Connect PWM2 to RC7 (Pin 9)
' Connect PWM3 to RA2 (Pin 17)
' Connect PWM4 to RC1 (Pin 15)
' Connect SW1 to RA5 (Pin 2)
' Connect SW2 to RA1 (Pin 3)
' Connect SW3 to RA3 (Pin 4)

@ ERRORLEVEL -306            ' Turn off "Crossing page boundary" error message


' Define LCD registers and bits
Define        LCD_DREG        PORTB
Define        LCD_DBIT        4
Define        LCD_RSREG        PORTC
Define        LCD_RSBIT        3
Define        LCD_EREG        PORTC
Define        LCD_EBIT        6
Define        LCD_BITS        4
Define        LCD_LINES        4
Pause 500

DEFINE ADC_BITS 10                  'Set number of bits in result (8, 10 or 12)
DEFINE ADC_CLOCK 3                  'Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50              'Set sampling time in microseconds

TRISA = %00111011                  'Set PORTA.0, 1, 3, 4 & 5 as digital inputs             
TRISB = %00000000                  'Set PORTB as all digital outputs
TRISC = %00000101                  'Set PORTC.0 & 2 as digital inputs                 
ANSELA = %00000011                  'Set PORTA.0 & 2 as analog inputs
ANSELB = %00000000                  'Disable PORTB analog inputs
ANSELC = %00000101                  'Set PORTC.0 & 2 as analog inputs

ADCON0 = %00000001                  'Enable ADC
ADCON1 = %11010000                  'Right Justify
                                    'Fosc/16
                                    'Vref connected to Vdd                                 
FVRCON.7 = 0                        'Disable Fixed Voltage Reference
CM1CON0.7 = 0                                                'Disable Comparitor 1
CM2CON0.7 = 0                                                'Disable Comparitor 2
INTCON.7 = 0                                                'Disable Interrupts

T2CON = %00000110                                        'Enable TMR2 and set PreScale to 16

' Set up PWM Registers
PWM1CON = %11000000
PR2 = %11111111

POT1        VAR                WORD
POT2        VAR                WORD
POT3        VAR                WORD
POT4        VAR                WORD
ScaledPot1        VAR        WORD
Dummy        VAR                WORD


        LCDOUT $FE, $1
        LCDOUT $FE, $80, " ** BAZTRONICS **  "
        LCDOUT $FE, $C0, "RGB LED Controller "
        LCDOUT $FE, $94, "  Version 1.1    "
        LCDOUT $FE, $D4, "                  "
        Pause 2000
        LCDOUT $FE, $1

Start:
        ADCIN 0, POT1
        LCDOUT $FE, $80, "POT1 = ", DEC POT1, "    ", DEC ScaledPot1, " "
        ADCIN 1, POT2
        LCDOUT $FE, $C0, "POT2 = ", DEC POT2, " "
        ADCIN 4, POT3
        LCDOUT $FE, $94, "POT3 = ", DEC POT3, " "
        ADCIN 6, POT4
        LCDOUT $FE, $D4, "POT4 = ", DEC POT4, " "
        Pause 100
        PWM1DCH = POT1.HIGHBYTE
        PWM1DCL = POT1.LOWBYTE
GOTO Start

End

When I try to compile this code I get syntax error messages for the following lines:
PWM1CON = %11000000
PWM1DCH = POT1.HIGHBYTE
PWM1DCL = POT1.LOWBYTE

I cannot see anything wrong with the syntax in these three lines of code. Can anyone shed some light on what it is wrong?

I am using PBP3 (Version 3.0.5.4)) and MCS (Version 5.0.0.0)

Cheers
Barry
VK2XBP

Viewing all articles
Browse latest Browse all 4745

Trending Articles