From b8cc7d22ae11576cdf44ca2cd3b39d64e514a930 Mon Sep 17 00:00:00 2001 From: "R. Steve McKown" Date: Sat, 10 Dec 2011 19:44:37 -0700 Subject: [PATCH] Change to PIC16LF1933 See main.c for pin mappings. --- adc_random.c | 20 ++++++++++---------- btn.c | 19 +++++++++---------- btn.h | 4 ++-- main.c | 49 +++++++++++++++++++++++++++++-------------------- rgb.c | 27 ++++++++++++++------------- unused.h | 5 ++++- 6 files changed, 68 insertions(+), 56 deletions(-) diff --git a/adc_random.c b/adc_random.c index e5e7694..133fc86 100644 --- a/adc_random.c +++ b/adc_random.c @@ -10,21 +10,21 @@ unsigned char adc_random() { - static bit save_ansa2; - static bit save_trisa2; + static bit save_ansa0; + static bit save_trisa0; unsigned char accumulate = 0; /* Turn on the FVR, configured for 1.024V to the ADC */ FVRCON = 0b10000001; while (!FVRRDY); /* wait for ready signal */ - /* Configure RA2 (AN2) for ADC input */ - save_ansa2 = ANSA2; - ANSA2 = 1; - save_trisa2 = TRISA2; - TRISA2 = 1; + /* Configure RA0 (AN0) for ADC input */ + save_ansa0 = ANSA0; + ANSA0 = 1; + save_trisa0 = TRISA0; + TRISA0 = 1; ADCON1 = 0b11110011; /* Right justified result, Frc clk, FVR/Vss refs */ - ADCON0 = 0b00001001; /* Enable channel AN2, enable ADC */ + ADCON0 = 0b00000001; /* Enable channel AN0, enable ADC */ /* Sample the ADC several times, accumulating the LSB of the result */ for (unsigned i = 0; i < 128; i++) { @@ -37,8 +37,8 @@ unsigned char adc_random() /* Turn off ADC, FVR, and revert PORTA changes */ ADON = 0; FVREN = 0; - TRISA2 = save_trisa2; - ANSA2 = save_ansa2; + TRISA0 = save_trisa0; + ANSA0 = save_ansa0; /* Return the result */ return accumulate; diff --git a/btn.c b/btn.c index 2fefcbe..3745364 100644 --- a/btn.c +++ b/btn.c @@ -9,10 +9,10 @@ #include "tmr.h" #include "task.h" -/* All the buttons are on PORTB. RB0 is the push button. - * RB4 and RB5 are the rocker switch positions left and right, respectively. +/* All the buttons are on PORTB. RB1 is the push button. + * RB4 and RB2 are the rocker switch positions left and right, respectively. */ -#define ALLMASK 0b00110001 +#define ALLMASK 0b00010110 /* Initialize the button module */ void btn_init() @@ -28,7 +28,7 @@ void btn_init() void btn_pben() { ndi(); - IOCBP0 = 1; IOCBN0 = 1; IOCBF0 = 0; + IOCBP1 = 1; IOCBN1 = 1; IOCBF1 = 0; nei(); } @@ -36,7 +36,7 @@ void btn_pben() void btn_pbdis() { ndi(); - IOCBP0 = 0; IOCBN0 = 0; IOCBF0 = 0; + IOCBP1 = 0; IOCBN1 = 0; IOCBF1 = 0; nei(); } @@ -45,7 +45,7 @@ void btn_rsen() { ndi(); IOCBP4 = 1; IOCBN4 = 1; IOCBF4 = 0; - IOCBP5 = 1; IOCBN5 = 1; IOCBF5 = 0; + IOCBP2 = 1; IOCBN2 = 1; IOCBF2 = 0; nei(); } @@ -54,14 +54,14 @@ void btn_rsdis() { ndi(); IOCBP4 = 0; IOCBN4 = 0; IOCBF4 = 0; - IOCBP5 = 0; IOCBN5 = 0; IOCBF5 = 0; + IOCBP2 = 0; IOCBN2 = 0; IOCBF2 = 0; nei(); } void btn_isr() { if (IOCIF) { - if (IOCBF0) { + if (IOCBF1) { btn_pbdis(); tmr_start(TMR_BTN_PB, 2); } @@ -69,7 +69,7 @@ void btn_isr() btn_rsdis(); tmr_start(TMR_BTN_RS, 2); } - if (IOCBF5) { + if (IOCBF2) { btn_rsdis(); tmr_start(TMR_BTN_RS, 2); } @@ -80,4 +80,3 @@ void btn_isr() if (tmr_fired(TMR_BTN_RS)) task_post(TASK_BTN_RS); } - diff --git a/btn.h b/btn.h index 8f0e3c1..5d4e749 100644 --- a/btn.h +++ b/btn.h @@ -28,10 +28,10 @@ enum { }; /* Read the current state of the pushbutton */ -#define btn_pb() (RB0) +#define btn_pb() (RB1) /* Read the current state of the rocker switch */ -#define btn_rs() (RB4 + ((unsigned char)RB5 << 1)) +#define btn_rs() (RB4 + ((unsigned char)RB2 << 1)) /* Enable the pushbutton from user code */ void btn_pben(); diff --git a/main.c b/main.c index 1df9445..6d20042 100644 --- a/main.c +++ b/main.c @@ -3,26 +3,35 @@ * * PWM test program * - * PIC resources in use, 18-pin DIP, by pin: - * - ( 1) RA2 - unused - * - ( 2) RA3 - CCP3 PWM for blu LED - * - ( 3) RA4 - CCP4 PWM for wht LED. ICSP pin 6, PGM/LVP. - * - ( 4) RA5 - ICSP pin 1, MCLR#/Vpp - * - ( 5) Vss - * - ( 6) RB0 - pushbutton - * - ( 7) RB1 - unused - * - ( 8) RB2 - unused - * - ( 9) RB3 - CCP1 PWM for red LED - * - (10) RB4 - rocker switch position B (right) - * - (11) RB5 - rocker switch position A (left) - * - (12) RB6 - unused (future crystal) - * - (13) RB7 - unused (future crystal) - * - (14) Vdd - * - (15) RA6 - unused - * - (16) RA7 - CCP2 PWM for grn LED - * - (17) RA0 - unused - * - (18) RA1 - unused - * + * PIC resources in use, 28-pin DIP, by pin: + * - ( 1) RE3 - Vpp/MCLR# + * - ( 2) RA0 - unused + * - ( 3) RA1 - unused + * - ( 4) RA2 - unused + * - ( 5) RA3 - unused + * - ( 6) RA4 - unused (could become PGM/LVP, but apparently not needed) + * - ( 7) RA5 - unused + * - ( 8) Vss + * - ( 9) RA7 - unused + * - (10) RA6 - unused + * - (11) RC0 - T1OSO + * - (12) RC1 - T1OSI + * - (13) RC2 - CCP1 PWM for red LED + * - (14) RC3 - unused + * - (15) RC4 - unused + * - (16) RC5 - unused + * - (17) RC6 - unused + * - (18) RC7 - unused + * - (19) Vss + * - (20) Vdd + * - (21) RB0 - CCP4 PWM for wht LED + * - (22) RB1 - Pushbutton + * - (23) RB2 - Rocker switch, right + * - (24) RB3 - CCP2 PWM for grn LED + * - (25) RB4 - Rocker switch, left + * - (26) RB5 - CCP3 PWM for blu LED + * - (27) RB6 - ICSPCLK + * - (28) RB7 - ICSPDAT */ #include diff --git a/rgb.c b/rgb.c index 8525442..151182c 100644 --- a/rgb.c +++ b/rgb.c @@ -6,13 +6,13 @@ #include -#define PINS_PORTA 0b10011000 -#define PINS_PORTB 0b00001000 +#define PINS_PORTB 0b00101001 +#define PINS_PORTC 0b00000100 void rgb_init() { /* Initialize rgb - * CCP1 on RB3, CCP2 on RA7, CCP3 on RA3, CCP4 on RA4 + * CCP1 on RC2, CCP2 on RB3, CCP3 on RB5, CCP4 on RB0 * - Fosc = 8 MHz, 4 MHz, 2 MHz * - Prescale = 4, 1, 1 * - PRx value = 0xff @@ -20,30 +20,31 @@ void rgb_init() */ /* Set rgb Rxn pins as outputs. */ - PORTA &= ~PINS_PORTA; - TRISA &= ~PINS_PORTA; PORTB &= ~PINS_PORTB; TRISB &= ~PINS_PORTB; + PORTC &= ~PINS_PORTC; + TRISC &= ~PINS_PORTC; - /* Configure ECCP1 */ + /* Configure CCP1 */ CCP1CON = 0b00001100; - CCPR1L = 0; /* Initial rgb value; only using 8 LSBs */ + CCPR1L = 0; /* Initial red value; only using 8 LSBs */ - /* Configure ECCP2 */ - APFCON0 |= 0b00001000; /* Use alternate output pin RA7 */ + /* Configure CCP2 */ + CCP2SEL = 1; /* CCP2 is on RB3 */ CCP2CON = 0b00001100; - CCPR2L = 0; /* Initial rgb value; only using 8 LSBs */ + CCPR2L = 0; /* Initial grn value; only using 8 LSBs */ /* Configure CCP3 */ + CCP3SEL = 1; /* CCP2 is on RB5 */ CCP3CON = 0b00001100; - CCPR3L = 0; /* Initial rgb value; only using 8 LSBs */ + CCPR3L = 0; /* Initial blu value; only using 8 LSBs */ /* Configure CCP4 */ CCP4CON = 0b00001100; - CCPR4L = 0; /* Initial rgb value; only using 8 LSBs */ + CCPR4L = 0; /* Initial wht value; only using 8 LSBs */ /* Configure Timer2. Start it to set 0 outputs, then stop it. */ - CCPTMRS = 0; /* All CCPx use Timer 2 */ + CCPTMRS0 = 0; /* CCP[1-4] use Timer 2 */ TMR2IF = 0; PR2 = 0xff; diff --git a/unused.h b/unused.h index d957520..98cca77 100644 --- a/unused.h +++ b/unused.h @@ -10,13 +10,16 @@ #include -#define UNUSED_PORTA 0b01000111 +#define UNUSED_PORTA 0b11111111 +#define UNUSED_PORTC 0b11111000 /* Initialize unused resources. */ #define unused_init() do { \ nWPUEN = 0; /* enable weak pull-ups on PortB */ \ PORTA |= UNUSED_PORTA; /* set unused PortA pins ... */ \ TRISA &= ~UNUSED_PORTA; /* ... to output high. */ \ + PORTC |= UNUSED_PORTC; /* set unused PortC pins ... */ \ + TRISC &= ~UNUSED_PORTC; /* ... to output high. */ \ } while (0) #endif -- 2.39.2