1 | /******************************************************************************/
|
2 | /* Files to Include */
|
3 | /******************************************************************************/
|
4 |
|
5 | #include <p30F4011.h> /* Device header file */
|
6 | #include <stdint.h> /* For uint32_t definition */
|
7 | #include <stdbool.h> /* For true/false definition */
|
8 | #include <libpic30.h>
|
9 | /* variables/params used by user.c */
|
10 |
|
11 | /******************************************************************************/
|
12 | /* User Functions */
|
13 | /******************************************************************************/
|
14 |
|
15 | /* <Initialize variables in user.h and insert code for user algorithms.> */
|
16 |
|
17 | /* TODO Initialize User Ports/Peripherals/Project here */
|
18 |
|
19 | void InitApp(void)
|
20 | {
|
21 | /* Setup analog functionality and port direction */
|
22 | /*Port B Init*/
|
23 | LATB = 0;
|
24 | TRISB = 0b111000000;
|
25 | /* Configure AN6 as analog inputs*/
|
26 | ADPCFG = 0xFFBF; // PORTB pins AN6 is analog input
|
27 | ADCON1 = 0x0000; // Manually clear SAMP to end sampling, start conversion
|
28 | ADCON2 = 0x0100; // Voltage reference from AVDD and AVSS
|
29 | ADCON3 = 0x0005; // Manual Sample, ADCS=5 -> Tad = 3*Tcy
|
30 | /*Configure PWM for free running mode
|
31 | PWM period = Tcy * prescale * PTPER = 0.33ns * 64 * PTPER
|
32 | PWM pulse width = (Tcy/2) * prescale * PDC*/
|
33 |
|
34 | PWMCON1 = 0x00FF; // Enable all PWM pairs in complementary mode
|
35 | PTCON = 0;
|
36 | _PTCKPS = 3; // prescale=1:64 (0=1:1, 1=1:4, 2=1:16, 3=1:64)
|
37 | PTPER = 9470; // 20ms PWM period (15-bit period value)
|
38 | PDC1 = PTPER; // 50% duty cycle on PWM channel 1
|
39 | PDC2 = PTPER; // 50% duty cycle on PWM channel 2
|
40 | PDC3 = PTPER; // 50% duty cycle on PWM channel 3
|
41 | _PTEN = 1; // Enable PWM time base
|
42 | /*Configure Timer 1
|
43 | In this example, I'm setting PR1 and TCKPS for 8Hz*/
|
44 | T1CON = 0; // Clear the Timer 1 configuration
|
45 | TMR1 = 0; // Reset Timer 1 counter
|
46 | PR1 = 14648; // Set the Timer 1 period (max 65535)
|
47 | T1CONbits.TCS = 0; // Select internal clock (Fosc/4)
|
48 | T1CONbits.TCKPS = 3; // Prescaler (0=1:1, 1=1:8, 2=1:64, 3=1:256)
|
49 | _T1IP = 1; // Set the Timer 1 interrupt priority
|
50 | _T1IF = 0; // Clear the Timer 1 interrupt flag
|
51 | _T1IE = 1; // Enable Timer 1 interrupt
|
52 | T1CONbits.TON = 1; // Turn on Timer 1
|
53 | /*LCD Init*/
|
54 |
|
55 | __delay32(6000);
|
56 | LATBbits.LATB0=1;
|
57 | void BIT_Send(Init_4Bit);
|
58 | __delay32(3500);
|
59 | void BIT_Send(Init_4BitEN);
|
60 | __delay32(3500);
|
61 | void BIT_Send(Init_4Bit);
|
62 | __delay32(3500);
|
63 | void BIT_Send(Init_4BitEN);
|
64 | __delay32(3500);
|
65 | void BIT_Send(Init_Line);
|
66 | __delay32(3500);
|
67 | void BIT_Send(Init_LineEN);
|
68 | __delay32(3500);
|
69 | void BIT_Send(Init_Disp);
|
70 | __delay32(3500);
|
71 | void BIT_Send(Init_DispEN);
|
72 | __delay32(3500);
|
73 | void BIT_Send(Init_Cursor1);
|
74 | __delay32(3500);
|
75 | void BIT_Send(Init_Cursor1EN);
|
76 | __delay32(3500);
|
77 | void BIT_Send(Init_Disp);
|
78 | __delay32(3500);
|
79 | void BIT_Send(Init_DispEN);
|
80 | __delay32(3500);
|
81 | void BIT_Send(Init_Clear);
|
82 | __delay32(3500);
|
83 | void BIT_Send(Init_ClearEN);
|
84 | __delay32(3500);
|
85 | void BIT_Send(Init_Disp);
|
86 | __delay32(3500);
|
87 | void BIT_Send(Init_DispEN);
|
88 | __delay32(3500);
|
89 | void BIT_Send(Init_Line);
|
90 | __delay32(3500);
|
91 | void BIT_Send(Init_LineEN);
|
92 | __delay32(3500);
|
93 |
|
94 |
|
95 | /* Initialize peripherals */
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 | /* This function reads a single sample from the specified
|
101 | analog input. It should take less than 2.5us if the chip
|
102 | is running at about 30 MIPS.
|
103 | Because the dsPIC30F4011 has a 10-bit ADC, the value
|
104 | returned will be between 0 and 1023.*/
|
105 | signed int read_analog_channel(int channel)
|
106 | {
|
107 |
|
108 | ADCHS = channel; // Select the requested channel
|
109 | ADCON1bits.ADON = 1; // Turn ADC ON
|
110 | ADCON1bits.SAMP = 1; // Start sampling
|
111 | __delay32(30); // 1us delay @ 30 MIPS
|
112 | ADCON1bits.SAMP = 0; // Start Converting
|
113 | while (!ADCON1bits.DONE);
|
114 | ADCON1bits.ADON = 0; // Turn ADC ON
|
115 | return ADCBUF0;
|
116 | }
|
117 | void Tick(void)
|
118 | {
|
119 | __delay32(30);
|
120 | LATBbits.LATB4 = 0;
|
121 | __delay32(30);
|
122 | LATBbits.LATB4 = 1;
|
123 | __delay32(30);
|
124 | LATBbits.LATB4 = 0;
|
125 | __delay32(30);
|
126 | }
|
127 | void Out_Tick(void)
|
128 | {
|
129 | __delay32(30);
|
130 | LATBbits.LATB5 = 0;
|
131 | __delay32(30);
|
132 | LATBbits.LATB5 = 1;
|
133 | __delay32(30);
|
134 | LATBbits.LATB5 = 0;
|
135 | __delay32(30);
|
136 | }
|
137 | void Send_Data(void)
|
138 | {
|
139 |
|
140 |
|
141 | }
|
142 | void BIT_Send(char Init[])
|
143 | {
|
144 |
|
145 | int i;
|
146 | for (i=0;i<9;i++)
|
147 | {
|
148 | LATBbits.LATB3 = Init[i];
|
149 | Tick();
|
150 | }
|
151 | Out_Tick();
|
152 | while(1);
|
153 | }
|