Bonjour à tous,
Mon nom est Xavier et cette année j'ai repris mes études pour effectuer une licence pro en alternance, dans le domaine de l'informatique industrielle et des automatismes.
J'ai commencé la programmation en C cette année et j'avoue avoir quelques difficultées, notament pour ce nouveau projet que mon entreprise m'a demandé de réaliser.
Ce projet consiste, avec un PIC16f887, de réaliser un convertion analogique numérique, avec le MCP3208 (ADC sur 112bits), qui communique avec le PIC via le module SPI, et d'afficher le résultat sur un écran lcd utilisant le module SPI également.
Le problème que je rencontre est dans la réception des données reçue par le PIC. En effet en recopiant l'etat de sortie de L'ADC sur les leds du PORTB et du PORTD, je me suis apercue qu'il me manquait le 12ème bit(MSB).
J'ai tester plusieurs configuration du mode SPI mais toujours pas de 12 éme bit.
Cela fait plusieurs jours que j'essai de récupérer ce bit manquant mais sans succé.
Voici mon code dans son intégralité. J'espère que vous y trouverez la ou les erreurs que j'ai commis pour me les signaler.
Je compte sur vous, je suis au fond, et merci d'avance à tous ceux qui prendront le temps de lire mon roman.
PS : je travaille avec la carte de developpement easy pic6 de chez MikroElectronika et le compilateur MicroC pro.
voici mon code:
Merci encore a vous tous pour votre patience et votre symphatie.Code://-------------------Port Expander module connections lcd---------------------- sbit SPExpanderCS at RA2_bit; sbit SPExpanderRST at RA3_bit; sbit SPExpanderCS_Direction at TRISA2_bit; sbit SPExpanderRST_Direction at TRISA3_bit; /**********************ADC module connections**********************************/ sbit Chip_Select at RC0_bit; sbit Chip_Select_Direction at TRISC0_bit; /**********************Déclaration des variables*******************************/ unsigned short value1,value2,value3,res,buffer = 0; unsigned int value; char *text; // variable pour l'affichage du texte bit oldstate; int i,b; unsigned long tlong,ch; /**********************Déclaration des prototypes de fonctions*****************/ void init_pic(void); void affichage (void); void convertion (void); void invertion (void); void Delay_us(); void Delay_ms(); unsigned short SPI1_Read(unsigned short buffer); void SPI1_Write(unsigned short data_); void SPI1_Init_Advanced(unsigned short master_slav, unsigned short data_sample, unsigned short clock_idle, unsigned short transmit_edge); /**********************Initialisation du PIC***********************************/ void init_pic() { ANSEL = 0; // All AN pin as digital ANSELH = 0; // All AN pin as digital C1ON_bit = 0; // Disable comparators C2ON_bit = 0; // Disable comparators PORTA = 0; // init value on PORTA PORTB = 0; // init value on PORTB PORTC = 0; // init value on PORTC PORTD = 0; // init value on PORTD PORTE = 0; // init value on PORTE TRISA = 0b11110011; // PORTA.2.3 are outputs others are inputs TRISB = 0; // PORTB is output TRISC = 0b11010110; // PORTC 5.3.0 are outputs others are inpouts TRISD = 0; // PORTD is output TRISE = 0xf; // PORTE is input Chip_Select = 1; // Deselect ADC } /************************fonction de convertion********************************/ void convertion() { SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, // Initialize SPI module _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_LOW_2_HIGH); while(1) { Chip_Select_Direction = 0; // Set CS# pin as Output Chip_Select = 0; // Select ADC SPI1_Write(0x07); // send the first byte configuratuion(single/CH7)5zero + start bit SPI1_Write(0xC0); // send the second byte configuratuion(single/CH7) SPI1_Write(0x00); // send the third byte with no effect value1 = SPI1_Read(buffer); // LSB first value2 = SPI1_Read(buffer); // MSB then Chip_Select = 1; // Deselect ADC b = value1; invertion(); value1 = res; b = value2; invertion(); value2 = res; PORTB = value1; // Send LSB to PORTB PORTD = value2; // Send MSB to PORTD affichage(); // Go to affichage oldstate = 0; } } /*****************************fonction d'affichage*****************************/ void affichage () { SPI_Lcd_Config(0); // Initialize Lcd over SPI interface SPExpanderCS = 0; // Select port expander SPI_Lcd_Cmd(_LCD_CLEAR); // Clear display SPI_Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off value = value2; value = (value<<8) | value1; text = "voltage:"; // assign text to string SPI_LCD_Out(1,1,text); // print string a on LCD, 2nd row, 1st column tlong = (long)value * 5000; // covert adc reading to milivolts tlong = tlong / 4095; // 0..4095 -> 0-5000mV ch = tlong / 1000; // extract volts digit SPI_LCD_Chr(1,9,48+ch); // write ASCII digit at 1st row, 9th column SPI_LCD_Chr_CP('.'); ch = (tlong / 100) % 10; // extract 0.1 volts digit SPI_LCD_Chr_CP(48+ch); // write ASCII digit at cursor point ch = (tlong / 10) % 10; // extract 0.01 volts digit SPI_LCD_Chr_CP(48+ch); // write ASCII digit at cursor point ch = tlong % 10; // extract 0.001 volts digit SPI_LCD_Chr_CP(48+ch); // write ASCII digit at cursor point SPI_LCD_Chr_CP('V'); Delay_ms(1); oldstate = 0; SPExpanderCS = 1; // Deselect port expander } /****************************swap byte****************************************/ void invertion () { res = 0; for ( i = 0; i < 8; ++i) { res = (res << 1); res |= (b & 1); b = (b >> 1); } } /********************** Programme principal ***********************************/ void main() { init_pic(); // mise en situation du PIC oldstate = 0; //do{ //if (Button(&PORTE, 0, 10, 1)) oldstate = 1; //if (oldstate && Button(&PORTE, 0, 10, 0)) { convertion(); //oldstate = 0; } //}while(1); }
-----