![]() |
LCD Library 1.2.0
LCD Library - LCD control class hierarchy library. Drop in replacement for the LiquidCrystal Library.
|
00001 // --------------------------------------------------------------------------- 00002 // Created by Florian Fida on 20/01/12 00003 // Copyright 2012 - Under creative commons license 3.0: 00004 // Attribution-ShareAlike CC BY-SA 00005 // http://creativecommons.org/licenses/by-sa/3.0/ 00006 // 00007 // This software is furnished "as is", without technical support, and with no 00008 // warranty, express or implied, as to its usefulness for any purpose. 00009 // --------------------------------------------------------------------------- 00010 // fio_shiftOut1 functions are based on Shif1 protocol developed by Roman Black 00011 // (http://www.romanblack.com/shift1.htm) 00012 // 00013 // Thread Safe: No 00014 // Extendable: Yes 00015 // 00016 // @file FastIO.h 00017 // This file implements basic fast IO routines. 00018 // 00019 // @brief 00020 // 00021 // @version API 1.0.0 00022 // 00023 // @author Florian Fida - 00024 // 00025 // @todo: 00026 // support chipkit: 00027 // (https://github.com/chipKIT32/chipKIT32-MAX/blob/master/hardware/pic32/ 00028 // cores/pic32/wiring_digital.c) 00029 // --------------------------------------------------------------------------- 00030 00031 #include "FastIO.h" 00032 00033 fio_register fio_pinToOutputRegister(uint8_t pin, uint8_t initial_state) 00034 { 00035 pinMode(pin, OUTPUT); 00036 00037 if(initial_state != SKIP) 00038 { 00039 digitalWrite(pin, initial_state); // also turns off pwm timer 00040 } 00041 #ifdef FIO_FALLBACK 00042 // just wasting memory if not using fast io... 00043 return 0; 00044 #else 00045 return portOutputRegister(digitalPinToPort(pin)); 00046 #endif 00047 } 00048 00049 fio_register fio_pinToInputRegister(uint8_t pin) 00050 { 00051 pinMode(pin, INPUT); 00052 digitalWrite(pin, LOW); // also turns off pwm timer and pullup 00053 #ifdef FIO_FALLBACK 00054 // just wasting memory if not using fast io... 00055 return 0; 00056 #else 00057 return portInputRegister(digitalPinToPort(pin)); 00058 #endif 00059 } 00060 00061 uint8_t fio_pinToBit(uint8_t pin) 00062 { 00063 #ifdef FIO_FALLBACK 00064 // (ab)use the bit variable to store the pin 00065 return pin; 00066 #else 00067 return digitalPinToBitMask(pin); 00068 #endif 00069 } 00070 00071 void fio_digitalWrite(fio_register pinRegister, uint8_t pinBit, uint8_t value) 00072 { 00073 #ifdef FIO_FALLBACK 00074 digitalWrite(pinBit, value); 00075 #else 00076 if(value == LOW) 00077 { 00078 fio_digitalWrite_LOW(pinRegister,pinBit); 00079 } 00080 else 00081 { 00082 fio_digitalWrite_HIGH(pinRegister,pinBit); 00083 } 00084 #endif 00085 } 00086 00087 int fio_digitalRead(fio_register pinRegister, uint8_t pinBit) 00088 { 00089 #ifdef FIO_FALLBACK 00090 return digitalRead (pinBit); 00091 #else 00092 if (*pinRegister & pinBit) 00093 { 00094 return HIGH; 00095 } 00096 return LOW; 00097 #endif 00098 } 00099 00100 void fio_shiftOut ( fio_register dataRegister, fio_bit dataBit, fio_register clockRegister, 00101 fio_bit clockBit, uint8_t value, uint8_t bitOrder) 00102 { 00103 // # disable interrupts 00104 // uint8_t oldSREG = SREG; 00105 // cli(); 00106 int8_t i; 00107 00108 for(i = 0; i < 8; i++) 00109 { 00110 if (bitOrder == LSBFIRST) 00111 { 00112 fio_digitalWrite(dataRegister, dataBit, !!(value & (1 << i))); 00113 } 00114 else 00115 { 00116 fio_digitalWrite(dataRegister, dataBit, !!(value & (1 << (7 - i)))); 00117 } 00118 fio_digitalWrite_HIGH (clockRegister, clockBit); 00119 // Switching is a little bit faster 00120 fio_digitalWrite_SWITCH (clockRegister,clockBit); 00121 } 00122 // # enable interrupts 00123 // SREG = oldSREG; 00124 } 00125 00126 void fio_shiftOut(fio_register dataRegister, uint8_t dataBit, fio_register clockRegister, uint8_t clockBit) 00127 { 00128 // shift out 0x0 (B00000000) fast, byte order is irrelevant 00129 fio_digitalWrite_LOW (dataRegister, dataBit); 00130 00131 for(uint8_t i = 0; i<8; ++i) 00132 { 00133 fio_digitalWrite_HIGH (clockRegister, clockBit); 00134 fio_digitalWrite_SWITCH (clockRegister, clockBit); 00135 } 00136 } 00137 void fio_shiftOut1_init(uint8_t pin) 00138 { 00139 fio_shiftOut1_init(fio_pinToOutputRegister(pin,HIGH),fio_pinToBit(pin)); 00140 } 00141 00142 void fio_shiftOut1_init(fio_register shift1Register, fio_bit shift1Bit) 00143 { 00144 // Make sure that capacitors are charged 00145 // 300us is an educated guess... 00146 fio_digitalWrite(shift1Register,shift1Bit,HIGH); 00147 delayMicroseconds(300); 00148 } 00149 void fio_shiftOut1(fio_register shift1Register, fio_bit shift1Bit, uint8_t value, boolean noLatch) 00150 { 00151 /* 00152 * this function are based on Shif1 protocol developed by Roman Black (http://www.romanblack.com/shift1.htm) 00153 * 00154 * test sketches: 00155 * http://pastebin.com/raw.php?i=2hnC9v2Z 00156 * http://pastebin.com/raw.php?i=bGg4DhXQ 00157 * http://pastebin.com/raw.php?i=tg1ZFiM5 00158 * http://pastebin.com/raw.php?i=93ExPDD3 - cascading 00159 * tested with: 00160 * TPIC6595N - seems to work fine (circuit: http://www.3guys1laser.com/ 00161 * arduino-one-wire-shift-register-prototype) 00162 * 7HC595N 00163 */ 00164 // disable interrupts since timing is going to be critical 00165 uint8_t oldSREG; 00166 oldSREG = SREG; 00167 cli(); 00168 00169 // iterate but ignore last bit (is it correct now?) 00170 for(int8_t i = 7; i>=0; --i) 00171 { 00172 00173 // assume that pin is HIGH (smokin' pot all day... :) - requires initialization 00174 if(LOW==!!(value & (1 << i))) 00175 { 00176 // LOW = 0 Bit 00177 fio_digitalWrite_SWITCHTO(shift1Register,shift1Bit,LOW); 00178 // hold pin LOW for 15us 00179 delayMicroseconds(15); 00180 fio_digitalWrite_SWITCHTO(shift1Register,shift1Bit,HIGH); 00181 // hold pin HIGH for 30us 00182 delayMicroseconds(30); 00183 } 00184 else 00185 { 00186 // HIGH = 1 Bit 00187 fio_digitalWrite_SWITCHTO(shift1Register,shift1Bit,LOW); 00188 //hold pin LOW for 1us - done! :) 00189 fio_digitalWrite_SWITCHTO(shift1Register,shift1Bit,HIGH); 00190 //hold pin HIGH for 15us 00191 delayMicroseconds(15); 00192 } 00193 if(!noLatch && i==1) 00194 { 00195 break; 00196 } 00197 } 00198 00199 if(!noLatch) 00200 { 00201 // send last bit (=LOW) and Latch command 00202 fio_digitalWrite_SWITCHTO(shift1Register,shift1Bit,LOW); 00203 // Hold pin low for 200us 00204 delayMicroseconds(199); 00205 fio_digitalWrite_HIGH(shift1Register,shift1Bit); 00206 // Hold pin high for 300us and leave it that way - using explicit HIGH here, just in case. 00207 delayMicroseconds(299); 00208 } 00209 00210 // enable interrupts 00211 SREG = oldSREG; 00212 00213 } 00214 void fio_shiftOut1(uint8_t pin, uint8_t value, boolean noLatch) 00215 { 00216 fio_shiftOut1(fio_pinToOutputRegister(pin, SKIP),fio_pinToBit(pin),value, noLatch); 00217 }