100% found this document useful (1 vote)
88 views

Simple Electroscope, C Code

The document contains code for initializing PWM and ADC functions on a PIC microcontroller. It sets up the pins for output and input, configures the registers for PWM period and duty cycle on one pin and analog to digital conversion on another pin. It then enters a loop to continuously sample the ADC and update the PWM output with the reading.

Uploaded by

Nolan Manteufel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
88 views

Simple Electroscope, C Code

The document contains code for initializing PWM and ADC functions on a PIC microcontroller. It sets up the pins for output and input, configures the registers for PWM period and duty cycle on one pin and analog to digital conversion on another pin. It then enters a loop to continuously sample the ADC and update the PWM output with the reading.

Uploaded by

Nolan Manteufel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

C: \ Document s and Set t i ngs\ Nobody\ My Document s\ Exer gy\ Vi va\ Header s\ Si mpl e El ect r scope\ Fi r mwar e\ mai n.

c
1 #i ncl ude <ht c. h>
2
3 void r eset _sensor ( void)
4 {
5 TRI SA1 = 0;
6 RA1 = 0;
7 _del ay( 1000) ;
8 }
9
10 void i ni t _vi va_pwm( void)
11 {
12 / / See page 91 i n PI C16F1829 dat asheet
13 / / Di sabl e TMR2' s i nt er r upt
14 PI E1, TMR2I E = 0;
15 / / See pages 219 and 125
16 / / Di sabl e RA5 ( CCP2) as out put ( by set t i ng i t t o i nput )
17 TRI SA5 = 1;
18 / / See page 122
19 / / Sel ect RA5 f or CCP2, al t er nat i vel y, RC3
20 APFCON1, CCP2SEL = 1;
21 / / See page 202
22 / / PWM per i od
23 PR2 = 0x80;
24 / / See page 236
25 CCP2CON = 0b00001111;
26 / / See page 36, not wel l document ed
27 / / Dut y Cycl e, smal l er - > br i ght er
28 CCPR2L = 0x00;
29 / / See page 237
30 / / Sel ect TMR2
31 CCPTMRS = 0x00;
32 / / See page 201
33 / / Conf i gur e and st ar t TMR2
34 T2CON = 0b00000101;
35 / / See page 219
36 / / Enabl e RA5 ( CCP2) as out put
37 TRI SA5 = 0;
38 }
39
40 void i ni t _vi va_adc( void)
41 {
42 / / See page 154 of PI C16F1829 dat asheet
43 / / Set RA1 as i nput
44 TRI SA1 = 1;
45 / / See page 126
46 / / Sel ect RA1 as ADC i nput
47 ANSELAbi t s. ANSA1 = 1;
48 / / See page 155
49 / / Conf i gur e ADCON0, do not set " GO" bi t
50 ADCON0 = 0b00000101;
51 / / See page 156
52 / / Conf i gur e ADCON1
53 ADCON1 = 0b00110000;
54 }
55
56 void mai n( void)
57 {
58 r eset _sensor ( ) ;
59 i ni t _vi va_adc( ) ;
60 i ni t _vi va_pwm( ) ;
61
62 while ( 1) {
63 _del ay( 10) ; / / Let ADC char ge
64 GO = 1; / / St ar t ADC conver si on
65 while ( GO) continue; / / Wai t unt i l conver si on compl et es
66 CCPR2L = ( ADRESH) ; / / Pl ace ADC r esul t i nt o PWM Dut y Cycl e
67 }
68 }
1

You might also like