1 | #include <pic.h>
|
2 | #include <stdlib.h>
|
3 | #include <stdio.h>
|
4 |
|
5 | /* PIC Configuration Bit:
|
6 | ** INTIO - Using Internal RC No Clock
|
7 | ** WDTDIS - Wacthdog Timer Disable
|
8 | ** PWRTEN - Power Up Timer Enable
|
9 | ** MCLREN - Master Clear Enable
|
10 | ** UNPROTECT - Code Un-Protect
|
11 | ** UNPROTECT - Data EEPROM Read Un-Protect
|
12 | ** BORDIS - Borwn Out Detect Disable
|
13 | ** IESODIS - Internal External Switch Over Mode Disable
|
14 | ** FCMDIS - Monitor Clock Fail Safe Disable
|
15 | */
|
16 | __CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT \
|
17 | & UNPROTECT & BORDIS & IESODIS & FCMDIS);
|
18 |
|
19 | // Using Internal Clock of 8 MHz
|
20 | #define FOSC 8000000L
|
21 | #define BAUD_RATE 9600
|
22 |
|
23 |
|
24 | // Delay Function
|
25 | #define _delay_us(x) { unsigned char us; \
|
26 | us = (x)/(12000000/FOSC)|1; \
|
27 | while(--us != 0) continue; }
|
28 |
|
29 | void _delay_ms(unsigned int ms)
|
30 | {
|
31 | unsigned char i;
|
32 | do {
|
33 | i = 4;
|
34 | do {
|
35 | _delay_us(164);
|
36 | } while(--i);
|
37 | } while(--ms);
|
38 | }
|
39 |
|
40 | // BRAM UART Debugging Function
|
41 |
|
42 | void ansi_cl(void)
|
43 | {
|
44 | // ANSI clear screen: cl=\E[H\E[J
|
45 | putchar(27);
|
46 | putchar('[');
|
47 | putchar('H');
|
48 | putchar(27);
|
49 | putchar('[');
|
50 | putchar('J');
|
51 | }
|
52 |
|
53 | void ansi_me(void)
|
54 | {
|
55 | // ANSI turn off all attribute: me=\E[0m
|
56 | putchar(27);
|
57 | putchar('[');
|
58 | putchar('0');
|
59 | putchar('m');
|
60 | }
|
61 |
|
62 | void uart_init(void)
|
63 | {
|
64 | TRISB5 = 1; // Set Port B5 and B7 for UART Tx and Rx
|
65 | TRISB7 = 0;
|
66 |
|
67 | // Baud Rate formula for SYNC=0 (Async), BRG16=0 (8-bit), BRGH=0 (low speed)
|
68 | // 0.16% Error for 8 MHz Oscilator Clock. Actual Rate will be 9615.
|
69 | // BAUD_RATE = FOSC / (64 x (SPBRG + 1))
|
70 |
|
71 | SPBRG = (int)(FOSC/(64UL * BAUD_RATE) - 1);
|
72 | TXSTA = 0b00100000; // Async, 8 bit and Enable Transmit (TXEN=1)
|
73 | RCSTA = 0b10010000; // Serial Port Enable, Async,8-bit and Enable Receipt (CREN=1)
|
74 | BAUDCTL=0;
|
75 | }
|
76 |
|
77 | void putch(unsigned char data)
|
78 | {
|
79 | // Send Data when TXIF bit is ready
|
80 | while(!TXIF) continue;
|
81 | TXREG = data;
|
82 | }
|
83 |
|
84 | unsigned char getch(void) {
|
85 | // Get Data when RCIF bit is ready
|
86 | while(!RCIF) continue;
|
87 | return RCREG;
|
88 | }
|
89 |
|
90 | void main(void)
|
91 | {
|
92 | ansi_me();
|
93 | ansi_cl();
|
94 |
|
95 | while (1)
|
96 | {
|
97 | OSCCON=0x70; // Select 8 MHz internal clock
|
98 |
|
99 | TRISA = 0x03; // Input for RA0 and RA1
|
100 | TRISC = 0x07; // Set RC0,RC1 and RC2 as input others as Output
|
101 | ANSEL = 0x71; // Set PORT AN0, AN4, AN5 and AN6 as analog input
|
102 | ANSELH = 0x00; // Set PORT AN8 to AN11 as Digital I/O
|
103 | PORTC = 0x00; // Turn Off all PORTC
|
104 | // Initial PIC16F690 UART
|
105 | uart_init();
|
106 |
|
107 | // Set the Attribute and Clear Screen
|
108 |
|
109 | printf("Welcome");
|
110 | putch("a");
|
111 | }
|
112 | }
|