See main.c for pin mappings.
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++) {
/* 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;
#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()
void btn_pben()
{
ndi();
- IOCBP0 = 1; IOCBN0 = 1; IOCBF0 = 0;
+ IOCBP1 = 1; IOCBN1 = 1; IOCBF1 = 0;
nei();
}
void btn_pbdis()
{
ndi();
- IOCBP0 = 0; IOCBN0 = 0; IOCBF0 = 0;
+ IOCBP1 = 0; IOCBN1 = 0; IOCBF1 = 0;
nei();
}
{
ndi();
IOCBP4 = 1; IOCBN4 = 1; IOCBF4 = 0;
- IOCBP5 = 1; IOCBN5 = 1; IOCBF5 = 0;
+ IOCBP2 = 1; IOCBN2 = 1; IOCBF2 = 0;
nei();
}
{
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);
}
btn_rsdis();
tmr_start(TMR_BTN_RS, 2);
}
- if (IOCBF5) {
+ if (IOCBF2) {
btn_rsdis();
tmr_start(TMR_BTN_RS, 2);
}
if (tmr_fired(TMR_BTN_RS))
task_post(TASK_BTN_RS);
}
-
};
/* 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();
*
* 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 <htc.h>
#include <htc.h>
-#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
*/
/* 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;
#include <htc.h>
-#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