Difference between revisions of "Blog 20"
Line 161: | Line 161: | ||
const int SPIMISO = 12; |
const int SPIMISO = 12; |
||
const int SPISS = 10; |
const int SPISS = 10; |
||
Will tell to all the pins, that they should become outputs. It after that writings the value 1 (On) to Slave Select, to make a HIGH value to the digital pin. |
Will tell to all the pins, that they should become outputs. It after that writings the value 1 (On) to Slave Select, to make a HIGH value to the digital pin. |
||
Line 195: | Line 195: | ||
return (received); |
return (received); |
||
} |
} |
||
When spi starts spkt it will write Slave select 0 and after that it will make it 1 again. |
When spi starts spkt it will write Slave select 0 and after that it will make it 1 again. |
||
Line 207: | Line 207: | ||
digitalWrite (SPISS, 1); |
digitalWrite (SPISS, 1); |
||
} |
} |
||
Name the address 0x82, that can't be changed. |
Name the address 0x82, that can't be changed. |
||
Line 216: | Line 216: | ||
#define WAIT1 25 |
#define WAIT1 25 |
||
#define WAIT2 15 |
#define WAIT2 15 |
||
Send to the lcd. |
Send to the lcd. |
||
Line 254: | Line 254: | ||
write_lcd (s); |
write_lcd (s); |
||
} |
} |
||
(cls = Clear Screen ) |
(cls = Clear Screen ) |
||
Line 277: | Line 277: | ||
SPI_endpkt (); |
SPI_endpkt (); |
||
} |
} |
||
Make clear what the addresses are from the servo and 7FETs. |
Make clear what the addresses are from the servo and 7FETs. |
||
static unsigned char spi_servo_addr = 0x86; |
static unsigned char spi_servo_addr = 0x86; |
||
static unsigned char spi_7fet_addr = 0x88; |
static unsigned char spi_7fet_addr = 0x88; |
||
void set_servo_var (unsigned char a, unsigned char v) |
void set_servo_var (unsigned char a, unsigned char v) |
||
{ |
{ |
||
Line 292: | Line 292: | ||
set_var (spi_7fet_addr, a, v); |
set_var (spi_7fet_addr, a, v); |
||
} |
} |
||
DDRC - The Port C Data Direction Register - read/write |
DDRC - The Port C Data Direction Register - read/write |
||
PORTC - The Port C Data Register - read/write |
PORTC - The Port C Data Register - read/write |
||
Line 311: | Line 311: | ||
cls_lcd (); |
cls_lcd (); |
||
} |
} |
||
static long makes that the number can't be negative. |
static long makes that the number can't be negative. |
||
Millis = Returns the number of milliseconds since the Arduino board began running the current program. |
Millis = Returns the number of milliseconds since the Arduino board began running the current program. |
Revision as of 14:51, 30 November 2015
!BETA!Understanding the I2C example code
Hello, in this post I want to give explanation tips for the example I2C code you can see LCD. A link to the code:
I would recommend if you are new to arduino to search stuff you are unfamiliar with on the internet.
On Arduino's reference page you can find a lot of the commands well explained.
What does the code? The code is a counter, that counts one up of the previous code. So, you can say it counts the seconds it is on.
The full code:
#include <Wire.h> void setup() { Wire.begin(); // join i2c bus (address optional for master) } #define I2C_LCD_ADDR (0x82>>1) void lcd_cls (void) { Wire.beginTransmission(I2C_LCD_ADDR); // transmit to device #4 Wire.send (0x10); // Send dummy byte to port 10: clear screen. Wire.send (0); Wire.endTransmission(); // stop transmitting } void lcd_print (char *str) { Wire.beginTransmission(I2C_LCD_ADDR); // transmit to device #4 Wire.send (0x0); // Send dummy byte to port 10: clear screen. Wire.send (str); Wire.endTransmission(); // stop transmitting } void lcd_gotoxy (int x, int y) { Wire.beginTransmission(I2C_LCD_ADDR); // transmit to device #4 Wire.send (0x11); // Send dummy byte to port 10: clear screen. Wire.send ((y << 5) | (x&0x1f)); Wire.endTransmission(); // stop transmitting } void loop() { char buf[0x10]; static int x; lcd_cls (); lcd_gotoxy (x&0xf,1); lcd_print ("*"); lcd_gotoxy (0,0); sprintf (buf, "x is: %d", x++); lcd_print (buf); delay(1000); }
Open I2C Library:
#include <Wire.h>
Set up the contact with I2C Bus.
void setup() { Wire.begin(); // join i2c bus (address optional for master) }
Gives the address location.
#define I2C_LCD_ADDR (0x82>>1)
The 0x82 is in this example bit shifted(>>) to the right with one. Bit shifting in this example let a the bits go to the right. So easy example with a 4 bits: 0010 will become 0001. The hexadecimal result should be: 0x41. This is an easy technique if you know the address value, but don't want to figure out what the hexadecimal value is when you divide it with 2.
(0x82>>1)
(cls = clear screen) This part can be called later in the script. The begin transmission write to the address given from I2C_LCD_ADDR with register 0x10 and the value 0. This will clear the screen of the lcd. After that is stops transmitting data.
void lcd_cls (void) { Wire.beginTransmission(I2C_LCD_ADDR); // transmit to device #4 Wire.send (0x10); // Send dummy byte to port 10: clear screen. Wire.send (0); Wire.endTransmission(); // stop transmitting }
This will put characters in a string. He again start transmitting to the address to register 0x0 and with the value given in the str.
void lcd_print (char *str) { Wire.beginTransmission(I2C_LCD_ADDR); // transmit to device #4 Wire.send (0x0); // Send dummy byte to port 10: clear screen. Wire.send (str); Wire.endTransmission(); // stop transmitting }
This will make contact to int x and int y. It will start transmitting to the address and talk to register 0x11. The bits value of y will be shifted 5 to the left. The '|' is an or function, what it does is that it wants one of or both of the two be 1. After that it will stop transmitting.
void lcd_gotoxy (int x, int y) { Wire.beginTransmission(I2C_LCD_ADDR); // transmit to device #4 Wire.send (0x11); // Send dummy byte to port 10: clear screen. Wire.send ((y << 5) | (x&0x1f)); Wire.endTransmission(); // stop transmitting }
The loop will make it that the arduino, will repeatedly repeat the code. The character in the array will get the in the string the value of '0x10'. The static make that x will only work with one function, and will stay with the same value. After that it makes contact with the cls function.
void loop() { char buf[0x10]; static int x; lcd_cls ();
Gives to lcd_gotoxy x & 0xf, and to the other one the value 1. Gives to lcd_print to the str, with 0x10. Gives to lcd_gotoxy the value 0,0 to clean it.
lcd_gotoxy (x&0xf,1); lcd_print ("*"); lcd_gotoxy (0,0);
It will print on the serial monitor(CTRL+M) with the array given value and will counts with x++ one more on the number. Let's the lcd print the given value in buf(Group or arrays with bytes). After that it waits 1000miliseconds = 1 seconds.
sprintf (buf, "x is: %d", x++); lcd_print (buf); delay(1000); }
Mistake in the code is in the comments, that there is several times said that: // Send dummy byte to port 10: clear screen. This only happens with the first one.
!BETA! Understanding the SPI example code
Here I will give extra information, about the SPI example you can see at LCD:
Arduino lcd SPI demo
I won't copy paste the full code, this time because I don't want to fill up the page. While I only explaining special parts of the code.
What does this code do? This code makes will count how long the code is running and print it out on the display. It counts in minutes, hours and days.
The const makes it that the 4 pins: CLK = Shift Clock MOSI = Master Out Slave Out MISO = Master In Slave Out SS = Slave Select will give a value that is read-only.
const int SPICLK = 13; const int SPIMOSI = 11; const int SPIMISO = 12; const int SPISS = 10;
Will tell to all the pins, that they should become outputs. It after that writings the value 1 (On) to Slave Select, to make a HIGH value to the digital pin.
(More about SPCR) SPCR = SPI Control Register The _BV is the bit value.~
- SPE - Enables the SPI when 1
- MSTR - Sets the Arduino in master mode when 1, slave mode when 0
- SPR1 and SPR0 - Sets the SPI speed, 00 is fastest (4MHz) 11 is slowest (250KHz)
void SPIinit(void) { pinMode (SPICLK, OUTPUT); pinMode (SPIMOSI, OUTPUT); pinMode (SPIMISO, OUTPUT); pinMode (SPISS, OUTPUT); digitalWrite (SPISS, 1); SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR1) | _BV(SPR0); }
character ~ When SPSR(SPI Status Register) & the bit value from SPIF(SPI Interrupt Flag) is 1, it will return the received SPDR(SPI Data Register) value.
char SPI(char d) { // send character over SPI char received = 0; SPDR = d; while(!(SPSR & _BV(SPIF))); received = SPDR; return (received); }
When spi starts spkt it will write Slave select 0 and after that it will make it 1 again.
void SPI_startpkt (void) { digitalWrite (SPISS, 0); } void SPI_endpkt (void) { digitalWrite (SPISS, 1); }
Name the address 0x82, that can't be changed.
static unsigned char addr = 0x82;
Makes WAIT1 be 25miliseconds, and WAIT2 be 15miliseconds.
#define WAIT1 25 #define WAIT2 15
Send to the lcd.
void send_lcd (unsigned char rnum, unsigned char len, char *s) {
SPI_startpkt (); delayMicroseconds (WAIT1); SPI (addr); delayMicroseconds (WAIT2); SPI (rnum); delayMicroseconds (WAIT2); while (len--) { SPI (*s++); delayMicroseconds (WAIT2); } SPI_endpkt (); }
Send to lcd string length
void write_lcd (char *s) { send_lcd (0, strlen (s), s); }
Unsigned char is an unsigned datatype, works the same as byte. Value y gets bit shifted 5 steps to the left. ( More about bit shifting you can read above at the I2C part.)
void write_at_lcd (unsigned char x, unsigned char y, char *s) { char c; c = (y << 5) | (x & 0x1f); send_lcd (0x11, 1, &c); write_lcd (s); }
(cls = Clear Screen )
void cls_lcd (void) { char c; send_lcd (0x10, 1, &c); }
Direction to set_var, where address, register and value can be given. It has several waits, that direct to the times given before at #WAIT1 and #WAIT2.
void set_var (unsigned char addr, unsigned char a, unsigned char v) { SPI_startpkt (); delayMicroseconds (WAIT1); SPI (addr); delayMicroseconds (WAIT2); SPI (a); delayMicroseconds (WAIT2); SPI (v); delayMicroseconds (WAIT2); SPI_endpkt (); }
Make clear what the addresses are from the servo and 7FETs.
static unsigned char spi_servo_addr = 0x86; static unsigned char spi_7fet_addr = 0x88;
void set_servo_var (unsigned char a, unsigned char v) { set_var (spi_servo_addr, a, v); } void set_7fet_var (unsigned char a, unsigned char v) { set_var (spi_7fet_addr, a, v); }
DDRC - The Port C Data Direction Register - read/write PORTC - The Port C Data Register - read/write PINC - The Port C Input Pins Register - read only
void setup (void) { int i; SPIinit (); Serial.begin (9600); DDRC = 0x30; PORTC = 0x20; for (i=0;i<10;i++) { PINC = 0x30; delay (300); } cls_lcd (); }
static long makes that the number can't be negative. Millis = Returns the number of milliseconds since the Arduino board began running the current program. So, if it is more than value next update 1000 milliseconds(1 second) it wil run the script. In the script it will count at s ( seconds ) 1. (s+) If S is higher than 59 it will set s back to 0. And will count 1 at the (m)minutes section. This works the same for the minutes section when it is geting above 59 it will count 1 to (h)hours.
void loop (void) { static long nextupdate = 1000; static unsigned char h, m, s; char buf[17]; if (millis () > nextupdate) { nextupdate += 1000; if (s < 59) s++; else { s = 0; if (m < 59) m++; else { m = 0; if (h < 23) h++; else { h = 0; // days ++; } } }
Here it will print out the given value from the if statement above. (%02d) And will print it out in 2 numbers on the display in decimals. Gives the values to open writing to display. And puts with buf the information in it from the arrays.
sprintf (buf, "%02d:%02d:%02d", h, m, s); write_at_lcd (8, 1, buf); Serial.write (buf); Serial.write ("\r\n"); // sprintf (buf, "%ld %ld", nextupdate, millis ()); // write_at_lcd (0, 0, buf); // delay (100); }
{ static unsigned char olds; if (olds != s) { olds = s; set_servo_var (0x20, (s & 0xf) << 4); } } { static unsigned char olds; if (olds != s) { static unsigned char values[4] = { 1, 4, 2, 8}; olds = s; set_7fet_var (0x10, values [s & 0x3]); } } }