#include #include "biosdem2.h" #define __DEBUG void init_i2c(void) { char x ; #ifdef __DEBUG TRISB3=0; // RA4 reflete l'état du dernier ACK du SONAR RB3 = 0 ; #define ACKLED RB3 #endif TRISC3 = 1 ; //SDA et SCL en entrée TRISC4 = 1 ; SSPSTAT = 0x80 ; // Pas de controle de slew rate, niveaux i2c SSPCON = 0x28 ; // enable i2c 7bits master mode SSPADD = 9 ; // fclock = 100khz = Fosc/(4(SSPADD+1) x = SSPBUF ; // vide le buffer et raz flags } char read_software_rev(void) { unsigned char srev ; SEN = 1; // send start bit while(SEN); // and wait for it to clear ACKDT = 0; // acknowledge bit ACKSTAT=1; SSPIF = 0; SSPBUF = 0xC4; // C4 - write command while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x01; // adress of cmd register while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif PEN = 1; // send stop bit while(PEN); // SEN = 1; // send start bit while(SEN); // and wait for it to clear ACKDT = 0; // acknowledge bit SSPIF = 0; SSPBUF = 0xC5; // 11000001 - read command while(!SSPIF); // wait for interrupt SSPIF = 0; // then clear it. RCEN = 1; // start receiving while(!STAT_BF); // wait for data srev = SSPBUF; // and get it ACKEN = 1; // start acknowledge sequence while(ACKEN); // wait for ack. sequence to end PEN = 1; // send stop bit while(PEN); // #ifdef __DEBUG ACKLED=1; #endif return srev ; } void say_ok ( void ) { SEN = 1; // send start bit while(SEN); // and wait for it to clear ACKDT = 0; // acknowledge bit ACKSTAT=1; SSPIF = 0; SSPBUF = 0xC4; // C4 - write command while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x00; // adress of cmd register while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x00; // nop while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x00; // vol max while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x05; // speed while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x03; // pitch while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 'O'; // 'o' while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 'K'; // 'k' while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x00; // null while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif PEN = 1; // send stop bit while(PEN); // SEN = 1; // send start bit while(SEN); // and wait for it to clear ACKDT = 0; // acknowledge bit ACKSTAT=1; SSPIF = 0; SSPBUF = 0xC4; // C4 - write command while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x00; // adress of cmd register while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x40; // speak buffer ! while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif PEN = 1; // send stop bit while(PEN); // } void say( unsigned char vol, unsigned char speed, unsigned char pitch, const char text[] ) { unsigned char i=0; #ifdef __DEBUG ACKLED=0; #endif SEN = 1; // send start bit while(SEN); // and wait for it to clear ACKDT = 0; // acknowledge bit ACKSTAT=1; SSPIF = 0; SSPBUF = 0xC4; // C4 - write command while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x00; // adress of cmd register while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x00; // nop while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = vol; // vol while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = speed; // speed while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = pitch; // pitch while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif while(text[i]!='\0') { SSPIF = 0; SSPBUF = text[i++]; while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif } SSPIF = 0; SSPBUF = 0x00; // null while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif PEN = 1; // send stop bit while(PEN); // SEN = 1; // send start bit while(SEN); // and wait for it to clear ACKDT = 0; // acknowledge bit ACKSTAT=1; SSPIF = 0; SSPBUF = 0xC4; // C4 - write command while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x00; // adress of cmd register while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 64; // speak buffer ! while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif PEN = 1; // send stop bit while(PEN); // } void stupid( void ) { SEN = 1; // send start bit while(SEN); // and wait for it to clear ACKDT = 0; // acknowledge bit ACKSTAT=1; SSPIF = 0; SSPBUF = 0xC4; // C4 - write command while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x00; // adress of cmd register while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x01; // while(!SSPIF); // SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif PEN = 1; // send stop bit while(PEN); // } char read_cmd_reg(void) { unsigned char cmd_reg ; SEN = 1; // send start bit while(SEN); // and wait for it to clear ACKDT = 0; // acknowledge bit ACKSTAT=1; SSPIF = 0; SSPBUF = 0xC4; // C4 - write command while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif SSPIF = 0; SSPBUF = 0x00; // adress of cmd register while(!SSPIF); // wait for interrupt SSPIF = 0; #ifdef __DEBUG ACKLED=ACKSTAT; ACKSTAT=1; #endif RSEN = 1; // send start bit while(RSEN); // and wait for it to clear ACKDT = 0; // acknowledge bit SSPIF = 0; SSPBUF = 0xC5; // 11000001 - read command while(!SSPIF); // wait for interrupt SSPIF = 0; // then clear it. RCEN = 1; // start receiving while(!STAT_BF); // wait for data cmd_reg = SSPBUF; // and get it DelayUs(100); ACKEN = 1; // start acknowledge sequence while(ACKEN); // wait for ack. sequence to end PEN = 1; // send stop bit while(PEN); // #ifdef __DEBUG ACKLED=1; #endif return cmd_reg ; }