1 | /* Bluetooth Controlled Car
|
2 | Accpets commands through UART, via GP-GC021 Bluetooth Module
|
3 | Control of head lights, brake lights, one drive motor and one
|
4 | steering servo are implemented.
|
5 | Clock frequency is 4MHz
|
6 | */
|
7 |
|
8 | #define HDLED PORTB.B7
|
9 | #define BRKLED PORTB.B6
|
10 | #define M1p PORTB.B5
|
11 | #define M1m PORTB.B4
|
12 | #define SERVO PORTB.B3
|
13 |
|
14 | unsigned short uart_rd;
|
15 | char pos;
|
16 |
|
17 | // Posistion Servo Rotor
|
18 | void position(){
|
19 | char i;
|
20 | SERVO = 1;
|
21 | // create ON-time delay in 10ths of a ms, delay in for-loop, hence less than 100ms
|
22 | for (i=0; i<=pos; i++) Delay_us(85);
|
23 | SERVO = 0;
|
24 | Delay_ms(12);
|
25 | }
|
26 |
|
27 | // Write outputs
|
28 | void action(){
|
29 | // simple error check
|
30 | if ((uart_rd & 0b00000011)>0){ // faulty byte received, since last two bits aren't used
|
31 | // clear outputs, center servo
|
32 | PORTB = 0;
|
33 | UART1_Write(0b00000011); // send error, ie faulty bits
|
34 | pos = 14; // compensate for off-center rotor placement, 1.5ms should be standard
|
35 | }else{
|
36 | // write outputs, place servo
|
37 | UART1_Write(uart_rd); // send response
|
38 | if ((uart_rd & 0b00001000)>0){
|
39 | pos = 9;
|
40 | }else if ((uart_rd & 0b00000100)>0){
|
41 | pos = 18;
|
42 | }else{
|
43 | pos = 14; // compensate for off-center rotor placement
|
44 | }
|
45 | PORTB = uart_rd & 0xF0;
|
46 | }
|
47 | }
|
48 |
|
49 | // Read UART and call action
|
50 | void main() {
|
51 | UART1_Init(9600); // Initialize UART module at 9600 bps
|
52 | Delay_ms(100); // Wait for UART module to stabilize
|
53 | DDRB = 0b11111000; // PORTB as output, save PB0, PB1 and PB2 which aren't used
|
54 | PORTB = 0; // Initialize portb
|
55 |
|
56 | while (1)
|
57 | {
|
58 | if (UART1_Data_Ready()) // If data is received,
|
59 | {
|
60 | uart_rd = UART1_Read(); // read the received data
|
61 | }
|
62 | action(); // write outputs
|
63 | position(); // posistion servo
|
64 | }
|
65 | }
|