| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | //***************************************************************************** |
| 2 | // File Name : basiciotest.c |
||
| 3 | // |
||
| 4 | // Title : example usage of basic input and output functions on the AVR |
||
| 5 | // Revision : 1.0 |
||
| 6 | // Notes : |
||
| 7 | // Target MCU : Atmel AVR series |
||
| 8 | // Editor Tabs : 4 |
||
| 9 | // |
||
| 10 | // Revision History: |
||
| 11 | // When Who Description of change |
||
| 12 | // ----------- ----------- ----------------------- |
||
| 13 | // 02-Feb-2003 pstang Created the program |
||
| 14 | //***************************************************************************** |
||
| 15 | |||
| 16 | |||
| 17 | //----- Include Files --------------------------------------------------------- |
||
| 18 | #include <avr/io.h> // include I/O definitions (port names, pin names, etc) |
||
| 19 | #include <avr/interrupt.h> // include interrupt support |
||
| 20 | |||
| 21 | #include "global.h" // include our global settings |
||
| 22 | //#include "uart.h" // include uart function library |
||
| 23 | //#include "rprintf.h" // include printf function library |
||
| 24 | //#include "timer.h" // include timer function library (timing, PWM, etc) |
||
| 25 | //#include "vt100.h" // include VT100 terminal support |
||
| 26 | //#include "encoder.h" // include encoder driver |
||
| 27 | |||
| 28 | |||
| 29 | //----- Begin Code ------------------------------------------------------------ |
||
| 30 | int main(void) |
||
| 31 | { |
||
| 32 | // initialize our libraries |
||
| 33 | // initialize the UART (serial port) |
||
| 34 | // uartInit(); |
||
| 35 | // set the baud rate of the UART for our debug/reporting output |
||
| 36 | // uartSetBaudRate(9600); |
||
| 37 | // initialize the timer system |
||
| 38 | // timerInit(); |
||
| 39 | // initialize rprintf system |
||
| 40 | // rprintfInit(uartSendByte); |
||
| 41 | // initialize vt100 library |
||
| 42 | // vt100Init(); |
||
| 43 | |||
| 44 | // clear the terminal screen |
||
| 45 | // vt100ClearScreen(); |
||
| 46 | |||
| 47 | u08 a; |
||
| 48 | |||
| 49 | // All AVR processors have I/O ports which each contain up to 8 |
||
| 50 | // user-controllable pins. From a hardware perspective, these I/O pins |
||
| 51 | // are each an actual physical pin coming out of the processor chip. |
||
| 52 | // The voltage on the pins can be sensed or controlled via software, |
||
| 53 | // hence their designation as Input/Output pins. |
||
| 54 | |||
| 55 | // While I/O pins are actual wires in the real world, they also exist |
||
| 56 | // inside the processor as special memory locations called registers. |
||
| 57 | // The software-controlled contents of these registers is what |
||
| 58 | // determines the state and operation of I/O pins and I/O ports. |
||
| 59 | |||
| 60 | // Since AVR processors deal naturally with 8 bits at a time, I/O pins |
||
| 61 | // are grouped into sets of 8 to form I/O ports. Three registers |
||
| 62 | // are assigned to each I/O port to control the function and state of |
||
| 63 | // that port's pins. The registers are 8-bits wide, and each bit (#0-7) |
||
| 64 | // determines the operation of the corresponding number pin (pin 0-7). |
||
| 65 | // The three registers are: |
||
| 66 | |||
| 67 | // DDRx - this register determines the direction (input/output) of the pins on port[x] |
||
| 68 | // A '0' bit in the DDR makes that port pin act as input |
||
| 69 | // A '1' bit in the DDR makes that port pin act as output |
||
| 70 | |||
| 71 | // PORTx - this register contains the output state of the pins on port[x] |
||
| 72 | // A '0' bit makes the port pin output a LOW (~0V) |
||
| 73 | // A '1' bit makes the port pin output a HIGH (~5V) |
||
| 74 | |||
| 75 | // PINx - this register contains the input state of the pins on port[x] |
||
| 76 | // A '0' bit indicates that the port pin is LOW (at ~0V) |
||
| 77 | // A '1' bit indicates that the port pin is HIGH (at ~5V) |
||
| 78 | |||
| 79 | // The x should be replaced with A,B,C,D,E,F, or G depending on the |
||
| 80 | // desired port. Note that not all AVR processors have the same set |
||
| 81 | // or number of ports. Consult the datasheet for your specific processor |
||
| 82 | // to find out which ports it has. |
||
| 83 | |||
| 84 | // in the AVR-GCC C language, ports can be accessed using two kinds of |
||
| 85 | // commands: |
||
| 86 | // inb() and outb() - in-byte and out-byte |
||
| 87 | // cbi() and sbi() - clear-bit and set-bit |
||
| 88 | |||
| 89 | // inb() and outb() should be used when you intend to read or write |
||
| 90 | // several bits of a register at once. Here are some examples: |
||
| 91 | |||
| 92 | outb(DDRA, 0x00); // set all port A pins to input |
||
| 93 | a = inb(PINA); // read the input state of all pins on port A |
||
| 94 | |||
| 95 | outb(DDRB, 0xFF); // set all port B pins to output |
||
| 96 | outb(PORTB, 0xF0); // set PB4-7 to HIGH and PB0-3 to LOW |
||
| 97 | |||
| 98 | // Often you may wish to change only a single bit in the registers |
||
| 99 | // while leaving the rest unaltered. For this, use cbi() and sbi(). |
||
| 100 | // For example: |
||
| 101 | |||
| 102 | sbi(DDRC, 0); // sets PC0 to be an output |
||
| 103 | sbi(DDRC, 1); // sets PC1 to be an output |
||
| 104 | |||
| 105 | cbi(PORTC, 1); // sets PC1 to output a LOW without altering any other pin |
||
| 106 | |||
| 107 | // the lines below will cause PC0 to pulse twice, |
||
| 108 | // but will leave all other port C pins unchanged |
||
| 109 | cbi(PORTC, 0); // sets PC0 to output a LOW |
||
| 110 | sbi(PORTC, 0); // sets PC0 to output a HIGH |
||
| 111 | cbi(PORTC, 0); // sets PC0 to output a LOW |
||
| 112 | sbi(PORTC, 0); // sets PC0 to output a HIGH |
||
| 113 | cbi(PORTC, 0); // sets PC0 to output a LOW |
||
| 114 | |||
| 115 | |||
| 116 | return 0; |
||
| 117 | } |
||
| 118 |
Powered by WebSVN v2.8.3