Programskim kodom se upravlja maketom malog DC motora koji je spojen na inkrementalni enkoder. Sa inkrementalnog enkodera se čitaju dvije faze impulsa pomoću kojih se računa brzina vrtnje iz impusla u vrenenu i smjer vrtnje iz odnosa stanja faza enkodera. Popunjenost PWM-a se zadaje potenciometrom a promjena smjera i pokretanje je se upravlja tipkalima.
CriusOLED.h
00001 /* ============================================ 00002 Modified Code from Crius 00003 The CriusOLED Hardware MUST be modified for correct function of ACK 00004 Copyright (c) 2014 Michael Ruck michael@ruck.com 00005 00006 Permission is hereby granted, free of charge, to any person obtaining a copy 00007 of this software and associated documentation files (the "Software"), to deal 00008 in the Software without restriction, including without limitation the rights 00009 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00010 copies of the Software, and to permit persons to whom the Software is 00011 furnished to do so, subject to the following conditions: 00012 00013 The above copyright notice and this permission notice shall be included in 00014 all copies or substantial portions of the Software. 00015 00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00019 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00020 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00021 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00022 THE SOFTWARE. 00023 =============================================== 00024 */ 00025 #include "data.h" 00026 #define OLED_ADDR (0x78) 00027 00028 I2C i2c(p9, p10); 00029 00030 void displayOn(void); 00031 void displayOff(void); 00032 void SendByte(unsigned char data); 00033 void sendcommand(unsigned char com); 00034 void SendByte(unsigned char com); 00035 void printBigNumber(unsigned char s, int X, int Y); 00036 void SendByteXY(unsigned char data, int X, int Y); 00037 00038 00039 //==========================================================// 00040 // Turns display on. 00041 void displayOn(void) 00042 { 00043 sendcommand(0xaf); //display on 00044 } 00045 00046 //==========================================================// 00047 // Turns display off. 00048 void displayOff(void) 00049 { 00050 sendcommand(0xae); //display off 00051 } 00052 //==========================================================// 00053 // Actually this sends a byte, not a char to draw in the display. 00054 // Display's chars uses 8 byte font the small ones and 96 bytes 00055 // for the big number font. 00056 void SendByte(unsigned char data) 00057 { 00058 //Create a temporary buffer 00059 char buff[2]; 00060 00061 //Load the control byte and 8-bit data 00062 buff[0] = (0x40); 00063 buff[1] = data; 00064 00065 //Write the data 00066 i2c.write(OLED_ADDR, buff, 2); 00067 00068 } 00069 00070 //==========================================================// 00071 // Used to send commands to the display. 00072 void sendcommand(unsigned char com) 00073 { 00074 //Create a temporary buffer 00075 char buff[2]; 00076 00077 //Load the control byte and 8-bit command 00078 buff[0] = 0x00; 00079 buff[1] = com; 00080 00081 //Write the command 00082 i2c.write(OLED_ADDR, buff, 2); 00083 } 00084 //==========================================================// 00085 // Set the cursor position in a 16 COL * 8 ROW map. 00086 void setXY(unsigned char row,unsigned char col) 00087 { 00088 sendcommand(0xb0+row); //set page address 00089 sendcommand(0x02+(8*col&0x0f)); //set low col address 00090 sendcommand(0x10+((8*col>>4)&0x0f)); //set high col address 00091 } 00092 //==========================================================// 00093 // Clears the display by sendind 0 to all the screen map. 00094 void clear_display(void) 00095 { 00096 unsigned char i,k; 00097 for(k=0; k<8; k++) { 00098 setXY(k,0); 00099 { 00100 for(i=0; i<128; i++) { //clear all COL 00101 SendByte(0); //clear all COL 00102 //delay(10); 00103 } 00104 } 00105 } 00106 } 00107 //==========================================================// 00108 // Resets display depending on the actual mode. 00109 void reset_display(void) 00110 { 00111 displayOff(); 00112 clear_display(); 00113 00114 00115 displayOn(); 00116 } 00117 00118 //==========================================================// 00119 void printBigTime(char *s) 00120 { 00121 00122 int Y=0; 00123 int lon = strlen(s); 00124 if(lon == 3) { 00125 Y = 0; 00126 } else if (lon == 2) { 00127 Y = 3; 00128 } else if (lon == 1) { 00129 Y = 6; 00130 } 00131 00132 int X = 2; 00133 while(*s) { 00134 printBigNumber(*s, X, Y); 00135 00136 Y+=3; 00137 X=2; 00138 setXY(X,Y); 00139 *s++; 00140 } 00141 } 00142 00143 00144 //==========================================================// 00145 // Prints a display big number (96 bytes) in coordinates X Y, 00146 // being multiples of 8. This means we have 16 COLS (0-15) 00147 // and 8 ROWS (0-7). 00148 void printBigNumber(unsigned char s, int X, int Y) 00149 { 00150 setXY(X,Y); 00151 int salto=0; 00152 for(int i=0; i<96; i++) { 00153 if(s == ' ') { 00154 SendByte(0); 00155 } else 00156 SendByte(bigNumbers[s-0x30][i]); 00157 00158 if(salto == 23) { 00159 salto = 0; 00160 X++; 00161 setXY(X,Y); 00162 } else { 00163 salto++; 00164 } 00165 } 00166 } 00167 //==========================================================// 00168 // Prints a display char (not just a byte) in coordinates X Y, 00169 // being multiples of 8. This means we have 16 COLS (0-15) 00170 // and 8 ROWS (0-7). 00171 void SendByteXY(unsigned char data, int X, int Y) 00172 { 00173 i2c.start(); 00174 i2c.write(OLED_ADDR); 00175 i2c.write(0x40); 00176 00177 for(int i=0; i<8; i++) 00178 i2c.write((int)(myFont[data-0x20]+i)); // <<------------------------------------- 00179 00180 i2c.stop(); // stop transmitting 00181 } 00182 00183 00184 00185 //==========================================================// 00186 // Prints a string regardless the cursor position. 00187 void sendStr(unsigned char *s) 00188 { 00189 unsigned char i=0; 00190 while(*s) { 00191 for(i=0; i<8; i++) { 00192 SendByte(myFont[*s-0x20][i]); 00193 } 00194 *s++; 00195 } 00196 } 00197 00198 00199 00200 00201 00202 00203 00204 //==========================================================// 00205 // Prints a string in coordinates X Y, being multiples of 8. 00206 // This means we have 16 COLS (0-15) and 8 ROWS (0-7). 00207 void sendStrXY( char *s, int X, int Y) 00208 { 00209 setXY(X,Y); 00210 unsigned char i=0; 00211 while(*s) { 00212 for(i=0; i<8; i++) { 00213 SendByte(myFont[*s-0x20][i]); 00214 } 00215 *s++; 00216 } 00217 } 00218 00219 00220 00221 //==========================================================// 00222 // Inits oled and draws logo at startup 00223 void init_OLED(void) 00224 { 00225 sendcommand(0xae); //display off 00226 sendcommand(0xa6); //Set Normal Display (default) 00227 // Adafruit Init sequence for 128x64 OLED module 00228 sendcommand(0xAE); //DISPLAYOFF 00229 sendcommand(0xD5); //SETDISPLAYCLOCKDIV 00230 sendcommand(0x80); // the suggested ratio 0x80 00231 sendcommand(0xA8); //SSD1306_SETMULTIPLEX 00232 sendcommand(0x3F); 00233 sendcommand(0xD3); //SETDISPLAYOFFSET 00234 sendcommand(0x0); //no offset 00235 sendcommand(0x40 | 0x0); //SETSTARTLINE 00236 sendcommand(0x8D); //CHARGEPUMP 00237 sendcommand(0x14); 00238 sendcommand(0x20); //MEMORYMODE 00239 sendcommand(0x00); //0x0 act like ks0108 00240 00241 sendcommand(0xA0 | 0x1); //SEGREMAP //Rotate screen 180 deg 00242 //sendcommand(0xA0); 00243 00244 sendcommand(0xC8); //COMSCANDEC Rotate screen 180 Deg 00245 //sendcommand(0xC0); 00246 00247 sendcommand(0xDA); //0xDA 00248 sendcommand(0x12); //COMSCANDEC 00249 sendcommand(0x81); //SETCONTRAS 00250 sendcommand(0xCF); // 00251 sendcommand(0xd9); //SETPRECHARGE 00252 sendcommand(0xF1); 00253 sendcommand(0xDB); //SETVCOMDETECT 00254 sendcommand(0x40); 00255 sendcommand(0xA4); //DISPLAYALLON_RESUME 00256 sendcommand(0xA6); //NORMALDISPLAY 00257 00258 clear_display(); 00259 sendcommand(0x2e); // stop scroll 00260 //----------------------------REVERSE comments----------------------------// 00261 // sendcommand(0xa0); //seg re-map 0->127(default) 00262 // sendcommand(0xa1); //seg re-map 127->0 00263 // sendcommand(0xc8); 00264 // delay(1000); 00265 //----------------------------REVERSE comments----------------------------// 00266 // sendcommand(0xa7); //Set Inverse Display 00267 // sendcommand(0xae); //display off 00268 sendcommand(0x20); //Set Memory Addressing Mode 00269 sendcommand(0x00); //Set Memory Addressing Mode ab Horizontal addressing mode 00270 // sendcommand(0x02); // Set Memory Addressing Mode ab Page addressing mode(RESET) 00271 00272 setXY(0,0); 00273 00274 sendcommand(0xaf); //display on 00275 00276 }
Generated on Thu Jul 14 2022 19:59:11 by 1.7.2