NSLU2 (Debian Slug) driving a PIC microprocessor as an i2c slave
With an i2c long line driver the i2c bus can extend for 100metres or more.
The Slug has no second rs232 port but several web sites say i2c slave mode for a PIC is difficult.
However this site explains how to do it - (many thanks!)
This introduced me to the hardware slave mode in the 16F690
- I was then pleased to find that my favourite PIC, the 16F877A also has the mode built in
Objective -
Control a PIC microprocessor by connecting it as an i2c slave to the Debian Slug i2c bus.
Use the PIC to control model servos - and all the other things a PIC can do.
As a first test I set up to drive two model servos and also detect the state of a switch on one of the PIC pins.
(Tip - use i2cdetect to find the PIC on the bus - Linux bit shifts the expected address and that can be very confusing!)
The CCS-C code for the PIC 16F690 (but also see § at bottom of page) #include <16F690.h> if(state < 0x80) //Master is sending data if(state == 2) //Second received byte is data } enable_interrupts(INT_SSP); enable_interrupts(GLOBAL); { //replace SERVO_CONTROLx by the chosen pin in all that follows the next 2 lines //servo needs 1000 to 2000 microseconds - (actually about 500 to 2400 on my servo) do output_high(SERVO_CONTROL2); delay_ms(10); } |
The CCS-C code for the PIC 16F877A INT16 DelayUsec1 , DelayUsec2 ; //delay 1000 to 2000 if(state < 0x80) //Master is sending data if(state == 2) //Second received byte is data } enable_interrupts(INT_SSP); enable_interrupts(GLOBAL); { //replace SERVO_CONTROL by the chosen pin in all that follows the next line //servo needs 1000 to 2000 microseconds do output_high(SERVO_CONTROL2); delay_ms(10); } |
The C code for the Slug Terminal - /* #include <stdio.h> int i2c; int rc; int main(int argc, char** argv) address = atoi(argv[1]); /* i2c decimal address is the first number after the program name */ i2c = open("/dev/i2c-0",O_RDWR); /* open the device dev/i2c-0 */ buf[0] = BufferAddress; /* address in buffer to start recording received data */ PicData = buf[0]; printf("PicData = %d \n", PicData); i2c = close(i2c); |
Matrix board layout for the 16F690
|
The 'scope output from the 16F877A Slug51local:/home/graham/i2c# ./16F690test3 80 0 100 200 (The same Slug code drives PICs 16F690 and 16F877A) |
PIC 16F877A as i2c slave printing to 4x20 LCD display and rs232 I have learned how to get the Slug to send bytes to the PIC and have the PIC send two byte AtoD numbers back to the Slug 16F877_AD_rs232_LCD_i2c_5.C CCS PIC program for 16F877A PIC_i2c.c Slug driver program Flex_LCD420.c my backup copy of this driver (with thanks!) with my settings to work with the above more detailed notes are here
|
§ No need to buy CCS C compiler for PIC i2c slave code?
I thought that CCS was the only compiler that helped you use a PIC as i2c slave.
I have now discovered that the free JAL compiler has been used to do that - I have not tested it yet
See http://www.lightbullet.com/download.php and Google for JAL
perhaps also
http://www.sirloon.net/loonaweb/sirblog/i2c-com-with-two-16f88-master-slave