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.

Committer:
mlucan
Date:
Tue Feb 11 09:55:03 2020 +0000
Revision:
0:a19b3eba5b1b
I2c oled, pisanje na ekran;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mlucan 0:a19b3eba5b1b 1 /* ============================================
mlucan 0:a19b3eba5b1b 2 Modified Code from Crius
mlucan 0:a19b3eba5b1b 3 The CriusOLED Hardware MUST be modified for correct function of ACK
mlucan 0:a19b3eba5b1b 4 Copyright (c) 2014 Michael Ruck michael@ruck.com
mlucan 0:a19b3eba5b1b 5
mlucan 0:a19b3eba5b1b 6 Permission is hereby granted, free of charge, to any person obtaining a copy
mlucan 0:a19b3eba5b1b 7 of this software and associated documentation files (the "Software"), to deal
mlucan 0:a19b3eba5b1b 8 in the Software without restriction, including without limitation the rights
mlucan 0:a19b3eba5b1b 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mlucan 0:a19b3eba5b1b 10 copies of the Software, and to permit persons to whom the Software is
mlucan 0:a19b3eba5b1b 11 furnished to do so, subject to the following conditions:
mlucan 0:a19b3eba5b1b 12
mlucan 0:a19b3eba5b1b 13 The above copyright notice and this permission notice shall be included in
mlucan 0:a19b3eba5b1b 14 all copies or substantial portions of the Software.
mlucan 0:a19b3eba5b1b 15
mlucan 0:a19b3eba5b1b 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mlucan 0:a19b3eba5b1b 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mlucan 0:a19b3eba5b1b 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mlucan 0:a19b3eba5b1b 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mlucan 0:a19b3eba5b1b 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mlucan 0:a19b3eba5b1b 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
mlucan 0:a19b3eba5b1b 22 THE SOFTWARE.
mlucan 0:a19b3eba5b1b 23 ===============================================
mlucan 0:a19b3eba5b1b 24 */
mlucan 0:a19b3eba5b1b 25 #include "data.h"
mlucan 0:a19b3eba5b1b 26 #define OLED_ADDR (0x78)
mlucan 0:a19b3eba5b1b 27
mlucan 0:a19b3eba5b1b 28 I2C i2c(p9, p10);
mlucan 0:a19b3eba5b1b 29
mlucan 0:a19b3eba5b1b 30 void displayOn(void);
mlucan 0:a19b3eba5b1b 31 void displayOff(void);
mlucan 0:a19b3eba5b1b 32 void SendByte(unsigned char data);
mlucan 0:a19b3eba5b1b 33 void sendcommand(unsigned char com);
mlucan 0:a19b3eba5b1b 34 void SendByte(unsigned char com);
mlucan 0:a19b3eba5b1b 35 void printBigNumber(unsigned char s, int X, int Y);
mlucan 0:a19b3eba5b1b 36 void SendByteXY(unsigned char data, int X, int Y);
mlucan 0:a19b3eba5b1b 37
mlucan 0:a19b3eba5b1b 38
mlucan 0:a19b3eba5b1b 39 //==========================================================//
mlucan 0:a19b3eba5b1b 40 // Turns display on.
mlucan 0:a19b3eba5b1b 41 void displayOn(void)
mlucan 0:a19b3eba5b1b 42 {
mlucan 0:a19b3eba5b1b 43 sendcommand(0xaf); //display on
mlucan 0:a19b3eba5b1b 44 }
mlucan 0:a19b3eba5b1b 45
mlucan 0:a19b3eba5b1b 46 //==========================================================//
mlucan 0:a19b3eba5b1b 47 // Turns display off.
mlucan 0:a19b3eba5b1b 48 void displayOff(void)
mlucan 0:a19b3eba5b1b 49 {
mlucan 0:a19b3eba5b1b 50 sendcommand(0xae); //display off
mlucan 0:a19b3eba5b1b 51 }
mlucan 0:a19b3eba5b1b 52 //==========================================================//
mlucan 0:a19b3eba5b1b 53 // Actually this sends a byte, not a char to draw in the display.
mlucan 0:a19b3eba5b1b 54 // Display's chars uses 8 byte font the small ones and 96 bytes
mlucan 0:a19b3eba5b1b 55 // for the big number font.
mlucan 0:a19b3eba5b1b 56 void SendByte(unsigned char data)
mlucan 0:a19b3eba5b1b 57 {
mlucan 0:a19b3eba5b1b 58 //Create a temporary buffer
mlucan 0:a19b3eba5b1b 59 char buff[2];
mlucan 0:a19b3eba5b1b 60
mlucan 0:a19b3eba5b1b 61 //Load the control byte and 8-bit data
mlucan 0:a19b3eba5b1b 62 buff[0] = (0x40);
mlucan 0:a19b3eba5b1b 63 buff[1] = data;
mlucan 0:a19b3eba5b1b 64
mlucan 0:a19b3eba5b1b 65 //Write the data
mlucan 0:a19b3eba5b1b 66 i2c.write(OLED_ADDR, buff, 2);
mlucan 0:a19b3eba5b1b 67
mlucan 0:a19b3eba5b1b 68 }
mlucan 0:a19b3eba5b1b 69
mlucan 0:a19b3eba5b1b 70 //==========================================================//
mlucan 0:a19b3eba5b1b 71 // Used to send commands to the display.
mlucan 0:a19b3eba5b1b 72 void sendcommand(unsigned char com)
mlucan 0:a19b3eba5b1b 73 {
mlucan 0:a19b3eba5b1b 74 //Create a temporary buffer
mlucan 0:a19b3eba5b1b 75 char buff[2];
mlucan 0:a19b3eba5b1b 76
mlucan 0:a19b3eba5b1b 77 //Load the control byte and 8-bit command
mlucan 0:a19b3eba5b1b 78 buff[0] = 0x00;
mlucan 0:a19b3eba5b1b 79 buff[1] = com;
mlucan 0:a19b3eba5b1b 80
mlucan 0:a19b3eba5b1b 81 //Write the command
mlucan 0:a19b3eba5b1b 82 i2c.write(OLED_ADDR, buff, 2);
mlucan 0:a19b3eba5b1b 83 }
mlucan 0:a19b3eba5b1b 84 //==========================================================//
mlucan 0:a19b3eba5b1b 85 // Set the cursor position in a 16 COL * 8 ROW map.
mlucan 0:a19b3eba5b1b 86 void setXY(unsigned char row,unsigned char col)
mlucan 0:a19b3eba5b1b 87 {
mlucan 0:a19b3eba5b1b 88 sendcommand(0xb0+row); //set page address
mlucan 0:a19b3eba5b1b 89 sendcommand(0x02+(8*col&0x0f)); //set low col address
mlucan 0:a19b3eba5b1b 90 sendcommand(0x10+((8*col>>4)&0x0f)); //set high col address
mlucan 0:a19b3eba5b1b 91 }
mlucan 0:a19b3eba5b1b 92 //==========================================================//
mlucan 0:a19b3eba5b1b 93 // Clears the display by sendind 0 to all the screen map.
mlucan 0:a19b3eba5b1b 94 void clear_display(void)
mlucan 0:a19b3eba5b1b 95 {
mlucan 0:a19b3eba5b1b 96 unsigned char i,k;
mlucan 0:a19b3eba5b1b 97 for(k=0; k<8; k++) {
mlucan 0:a19b3eba5b1b 98 setXY(k,0);
mlucan 0:a19b3eba5b1b 99 {
mlucan 0:a19b3eba5b1b 100 for(i=0; i<128; i++) { //clear all COL
mlucan 0:a19b3eba5b1b 101 SendByte(0); //clear all COL
mlucan 0:a19b3eba5b1b 102 //delay(10);
mlucan 0:a19b3eba5b1b 103 }
mlucan 0:a19b3eba5b1b 104 }
mlucan 0:a19b3eba5b1b 105 }
mlucan 0:a19b3eba5b1b 106 }
mlucan 0:a19b3eba5b1b 107 //==========================================================//
mlucan 0:a19b3eba5b1b 108 // Resets display depending on the actual mode.
mlucan 0:a19b3eba5b1b 109 void reset_display(void)
mlucan 0:a19b3eba5b1b 110 {
mlucan 0:a19b3eba5b1b 111 displayOff();
mlucan 0:a19b3eba5b1b 112 clear_display();
mlucan 0:a19b3eba5b1b 113
mlucan 0:a19b3eba5b1b 114
mlucan 0:a19b3eba5b1b 115 displayOn();
mlucan 0:a19b3eba5b1b 116 }
mlucan 0:a19b3eba5b1b 117
mlucan 0:a19b3eba5b1b 118 //==========================================================//
mlucan 0:a19b3eba5b1b 119 void printBigTime(char *s)
mlucan 0:a19b3eba5b1b 120 {
mlucan 0:a19b3eba5b1b 121
mlucan 0:a19b3eba5b1b 122 int Y=0;
mlucan 0:a19b3eba5b1b 123 int lon = strlen(s);
mlucan 0:a19b3eba5b1b 124 if(lon == 3) {
mlucan 0:a19b3eba5b1b 125 Y = 0;
mlucan 0:a19b3eba5b1b 126 } else if (lon == 2) {
mlucan 0:a19b3eba5b1b 127 Y = 3;
mlucan 0:a19b3eba5b1b 128 } else if (lon == 1) {
mlucan 0:a19b3eba5b1b 129 Y = 6;
mlucan 0:a19b3eba5b1b 130 }
mlucan 0:a19b3eba5b1b 131
mlucan 0:a19b3eba5b1b 132 int X = 2;
mlucan 0:a19b3eba5b1b 133 while(*s) {
mlucan 0:a19b3eba5b1b 134 printBigNumber(*s, X, Y);
mlucan 0:a19b3eba5b1b 135
mlucan 0:a19b3eba5b1b 136 Y+=3;
mlucan 0:a19b3eba5b1b 137 X=2;
mlucan 0:a19b3eba5b1b 138 setXY(X,Y);
mlucan 0:a19b3eba5b1b 139 *s++;
mlucan 0:a19b3eba5b1b 140 }
mlucan 0:a19b3eba5b1b 141 }
mlucan 0:a19b3eba5b1b 142
mlucan 0:a19b3eba5b1b 143
mlucan 0:a19b3eba5b1b 144 //==========================================================//
mlucan 0:a19b3eba5b1b 145 // Prints a display big number (96 bytes) in coordinates X Y,
mlucan 0:a19b3eba5b1b 146 // being multiples of 8. This means we have 16 COLS (0-15)
mlucan 0:a19b3eba5b1b 147 // and 8 ROWS (0-7).
mlucan 0:a19b3eba5b1b 148 void printBigNumber(unsigned char s, int X, int Y)
mlucan 0:a19b3eba5b1b 149 {
mlucan 0:a19b3eba5b1b 150 setXY(X,Y);
mlucan 0:a19b3eba5b1b 151 int salto=0;
mlucan 0:a19b3eba5b1b 152 for(int i=0; i<96; i++) {
mlucan 0:a19b3eba5b1b 153 if(s == ' ') {
mlucan 0:a19b3eba5b1b 154 SendByte(0);
mlucan 0:a19b3eba5b1b 155 } else
mlucan 0:a19b3eba5b1b 156 SendByte(bigNumbers[s-0x30][i]);
mlucan 0:a19b3eba5b1b 157
mlucan 0:a19b3eba5b1b 158 if(salto == 23) {
mlucan 0:a19b3eba5b1b 159 salto = 0;
mlucan 0:a19b3eba5b1b 160 X++;
mlucan 0:a19b3eba5b1b 161 setXY(X,Y);
mlucan 0:a19b3eba5b1b 162 } else {
mlucan 0:a19b3eba5b1b 163 salto++;
mlucan 0:a19b3eba5b1b 164 }
mlucan 0:a19b3eba5b1b 165 }
mlucan 0:a19b3eba5b1b 166 }
mlucan 0:a19b3eba5b1b 167 //==========================================================//
mlucan 0:a19b3eba5b1b 168 // Prints a display char (not just a byte) in coordinates X Y,
mlucan 0:a19b3eba5b1b 169 // being multiples of 8. This means we have 16 COLS (0-15)
mlucan 0:a19b3eba5b1b 170 // and 8 ROWS (0-7).
mlucan 0:a19b3eba5b1b 171 void SendByteXY(unsigned char data, int X, int Y)
mlucan 0:a19b3eba5b1b 172 {
mlucan 0:a19b3eba5b1b 173 i2c.start();
mlucan 0:a19b3eba5b1b 174 i2c.write(OLED_ADDR);
mlucan 0:a19b3eba5b1b 175 i2c.write(0x40);
mlucan 0:a19b3eba5b1b 176
mlucan 0:a19b3eba5b1b 177 for(int i=0; i<8; i++)
mlucan 0:a19b3eba5b1b 178 i2c.write((int)(myFont[data-0x20]+i)); // <<-------------------------------------
mlucan 0:a19b3eba5b1b 179
mlucan 0:a19b3eba5b1b 180 i2c.stop(); // stop transmitting
mlucan 0:a19b3eba5b1b 181 }
mlucan 0:a19b3eba5b1b 182
mlucan 0:a19b3eba5b1b 183
mlucan 0:a19b3eba5b1b 184
mlucan 0:a19b3eba5b1b 185 //==========================================================//
mlucan 0:a19b3eba5b1b 186 // Prints a string regardless the cursor position.
mlucan 0:a19b3eba5b1b 187 void sendStr(unsigned char *s)
mlucan 0:a19b3eba5b1b 188 {
mlucan 0:a19b3eba5b1b 189 unsigned char i=0;
mlucan 0:a19b3eba5b1b 190 while(*s) {
mlucan 0:a19b3eba5b1b 191 for(i=0; i<8; i++) {
mlucan 0:a19b3eba5b1b 192 SendByte(myFont[*s-0x20][i]);
mlucan 0:a19b3eba5b1b 193 }
mlucan 0:a19b3eba5b1b 194 *s++;
mlucan 0:a19b3eba5b1b 195 }
mlucan 0:a19b3eba5b1b 196 }
mlucan 0:a19b3eba5b1b 197
mlucan 0:a19b3eba5b1b 198
mlucan 0:a19b3eba5b1b 199
mlucan 0:a19b3eba5b1b 200
mlucan 0:a19b3eba5b1b 201
mlucan 0:a19b3eba5b1b 202
mlucan 0:a19b3eba5b1b 203
mlucan 0:a19b3eba5b1b 204 //==========================================================//
mlucan 0:a19b3eba5b1b 205 // Prints a string in coordinates X Y, being multiples of 8.
mlucan 0:a19b3eba5b1b 206 // This means we have 16 COLS (0-15) and 8 ROWS (0-7).
mlucan 0:a19b3eba5b1b 207 void sendStrXY( char *s, int X, int Y)
mlucan 0:a19b3eba5b1b 208 {
mlucan 0:a19b3eba5b1b 209 setXY(X,Y);
mlucan 0:a19b3eba5b1b 210 unsigned char i=0;
mlucan 0:a19b3eba5b1b 211 while(*s) {
mlucan 0:a19b3eba5b1b 212 for(i=0; i<8; i++) {
mlucan 0:a19b3eba5b1b 213 SendByte(myFont[*s-0x20][i]);
mlucan 0:a19b3eba5b1b 214 }
mlucan 0:a19b3eba5b1b 215 *s++;
mlucan 0:a19b3eba5b1b 216 }
mlucan 0:a19b3eba5b1b 217 }
mlucan 0:a19b3eba5b1b 218
mlucan 0:a19b3eba5b1b 219
mlucan 0:a19b3eba5b1b 220
mlucan 0:a19b3eba5b1b 221 //==========================================================//
mlucan 0:a19b3eba5b1b 222 // Inits oled and draws logo at startup
mlucan 0:a19b3eba5b1b 223 void init_OLED(void)
mlucan 0:a19b3eba5b1b 224 {
mlucan 0:a19b3eba5b1b 225 sendcommand(0xae); //display off
mlucan 0:a19b3eba5b1b 226 sendcommand(0xa6); //Set Normal Display (default)
mlucan 0:a19b3eba5b1b 227 // Adafruit Init sequence for 128x64 OLED module
mlucan 0:a19b3eba5b1b 228 sendcommand(0xAE); //DISPLAYOFF
mlucan 0:a19b3eba5b1b 229 sendcommand(0xD5); //SETDISPLAYCLOCKDIV
mlucan 0:a19b3eba5b1b 230 sendcommand(0x80); // the suggested ratio 0x80
mlucan 0:a19b3eba5b1b 231 sendcommand(0xA8); //SSD1306_SETMULTIPLEX
mlucan 0:a19b3eba5b1b 232 sendcommand(0x3F);
mlucan 0:a19b3eba5b1b 233 sendcommand(0xD3); //SETDISPLAYOFFSET
mlucan 0:a19b3eba5b1b 234 sendcommand(0x0); //no offset
mlucan 0:a19b3eba5b1b 235 sendcommand(0x40 | 0x0); //SETSTARTLINE
mlucan 0:a19b3eba5b1b 236 sendcommand(0x8D); //CHARGEPUMP
mlucan 0:a19b3eba5b1b 237 sendcommand(0x14);
mlucan 0:a19b3eba5b1b 238 sendcommand(0x20); //MEMORYMODE
mlucan 0:a19b3eba5b1b 239 sendcommand(0x00); //0x0 act like ks0108
mlucan 0:a19b3eba5b1b 240
mlucan 0:a19b3eba5b1b 241 sendcommand(0xA0 | 0x1); //SEGREMAP //Rotate screen 180 deg
mlucan 0:a19b3eba5b1b 242 //sendcommand(0xA0);
mlucan 0:a19b3eba5b1b 243
mlucan 0:a19b3eba5b1b 244 sendcommand(0xC8); //COMSCANDEC Rotate screen 180 Deg
mlucan 0:a19b3eba5b1b 245 //sendcommand(0xC0);
mlucan 0:a19b3eba5b1b 246
mlucan 0:a19b3eba5b1b 247 sendcommand(0xDA); //0xDA
mlucan 0:a19b3eba5b1b 248 sendcommand(0x12); //COMSCANDEC
mlucan 0:a19b3eba5b1b 249 sendcommand(0x81); //SETCONTRAS
mlucan 0:a19b3eba5b1b 250 sendcommand(0xCF); //
mlucan 0:a19b3eba5b1b 251 sendcommand(0xd9); //SETPRECHARGE
mlucan 0:a19b3eba5b1b 252 sendcommand(0xF1);
mlucan 0:a19b3eba5b1b 253 sendcommand(0xDB); //SETVCOMDETECT
mlucan 0:a19b3eba5b1b 254 sendcommand(0x40);
mlucan 0:a19b3eba5b1b 255 sendcommand(0xA4); //DISPLAYALLON_RESUME
mlucan 0:a19b3eba5b1b 256 sendcommand(0xA6); //NORMALDISPLAY
mlucan 0:a19b3eba5b1b 257
mlucan 0:a19b3eba5b1b 258 clear_display();
mlucan 0:a19b3eba5b1b 259 sendcommand(0x2e); // stop scroll
mlucan 0:a19b3eba5b1b 260 //----------------------------REVERSE comments----------------------------//
mlucan 0:a19b3eba5b1b 261 // sendcommand(0xa0); //seg re-map 0->127(default)
mlucan 0:a19b3eba5b1b 262 // sendcommand(0xa1); //seg re-map 127->0
mlucan 0:a19b3eba5b1b 263 // sendcommand(0xc8);
mlucan 0:a19b3eba5b1b 264 // delay(1000);
mlucan 0:a19b3eba5b1b 265 //----------------------------REVERSE comments----------------------------//
mlucan 0:a19b3eba5b1b 266 // sendcommand(0xa7); //Set Inverse Display
mlucan 0:a19b3eba5b1b 267 // sendcommand(0xae); //display off
mlucan 0:a19b3eba5b1b 268 sendcommand(0x20); //Set Memory Addressing Mode
mlucan 0:a19b3eba5b1b 269 sendcommand(0x00); //Set Memory Addressing Mode ab Horizontal addressing mode
mlucan 0:a19b3eba5b1b 270 // sendcommand(0x02); // Set Memory Addressing Mode ab Page addressing mode(RESET)
mlucan 0:a19b3eba5b1b 271
mlucan 0:a19b3eba5b1b 272 setXY(0,0);
mlucan 0:a19b3eba5b1b 273
mlucan 0:a19b3eba5b1b 274 sendcommand(0xaf); //display on
mlucan 0:a19b3eba5b1b 275
mlucan 0:a19b3eba5b1b 276 }