PIC18F4455 with 3310 LCD

General discussion on mikroC.
Post Reply
Author
Message
Vizier87
Posts: 51
Joined: 16 Jun 2011 11:49

PIC18F4455 with 3310 LCD

#1 Post by Vizier87 » 05 Jul 2011 05:19

Hi Guys, first post here.

I have been reading a lot of documentation on 3310 LCDs but I'm pretty dazzled by all the info. I'd just like to display some simple stuff, for a start and further my knowledge regarding the SPI protocols and so on. I'm using MikroC version 4.5.
My PIC's pin is connected to the LCD as follows:

SCK=RB1
SDIN=RC7
D/C=RB0
SCE=RB2
RESET=RB3.

i've tried several codes including the one from this forum but no avail. I suppose I just don't understand how the codes work, but please help me understand. I'm pretty well informed on the electronics part, just the programming itself baffles me.

I'd like the shortest form of code to help me understand the PCD8544's response to the SPI programming.

Thanks a lot.
Vortex Electrica Corp.

abcdefgg
Posts: 146
Joined: 19 Apr 2011 11:54
Location: Toko Japan

Re: PIC18F4455 with 3310 LCD

#2 Post by abcdefgg » 15 Jul 2011 07:23

Hi,

Driving a Nokia 3310 lcd is not that hard, but you need quite a lot of steps to make it work.

Do you only want to print text on it?

paulfjujo
Posts: 1544
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: PIC18F4455 with 3310 LCD

#3 Post by paulfjujo » 04 Sep 2011 16:35

hello,

You can find enclosed nokia LCD3310 program test, for 18F252
with also RS232 output , easier to debug !!
you can remove the ADC part for testing.
After Nokia init, you must get a voltage about 8V across the capacitor between pin7 and 0V.
The Nokia level are 3,3V, so you have to insert resistors between PIC output and LCD input.
example : R=2.7K .
My nokia is 8 lines of 16cars , some others are 6L x14 cars ?
I also have a MPlab C18 versus ...

You are unsing RC7 and RBx for nokia
its better to use the same port and avoid dedicated pin as RC6 RC7 , ofen dedicated for UART.

Code: Select all

/*   OK    version MCP3424 15 bits
// OK AVEC LCD 8x16 cars et RS232 !!!   pas d'usage de long mot 24 bits
// rajout synchro sur timer ..at 1 seconde
// 09 sept 2010  changement LCD nokia par un 8x16 car (Braderie de LILLE!)
// 28/08/10 bug sur n° de voie ??? => 4 1 2 3 pour CH 1 2 3 4
// scheme :18F258_10mhz.cfg
// 17/08/10 bad avec soft I2C sur RB1 RB4  ..mais OK sur RB6 et RB7 ????
// limite à 4 ch en differentiel ou mode commun
// 29/07/10 retour à Soft_I2C
// 23/7/10  usage librairy m_i2c_1.c  Version 0.1.1    Author: Michael Pearce
// Master Only mode - output only in this version
 22/07 modif port SDA SCL
 21/07/10      test interupt timer1
 03/07/10    Bug avec SoftwareI2C librairie     .......
 28 juin 2010
 ---------------------------------------------------------------------------
Interface to TinyBootLoader, v1.9.5
contact: claudiu.chiculita@ugal.ro
http://www.etc.ugal.ro/cchiculita/software/picbootloader.htm
--------------------------------------------
file: C:\_MickroC\_MesProjets\_MCP32424_18f258\MCP3424_18F.hex
Connected to \\.\COM2 at 19200
  HEX: 0 min old, INHX32,18Fcode+cfg, total=4837 bytes.
  Searching for PIC ...
  Found:18F 252o/452o
  WRITE OK  at 21:29,   time:4.787 sec
------------------------------------------------------------------------------

mikroCPIC1618.exe -MSF -DBG -pP18F258 -O11110012 -fo10 -
N"C:\_MickroC\_MesProjets\_MCP32424_18f258\MCP3424_18F.mcppi" -SP"E:\_save2010\mikroC PRO for
PIC\defs\" -SP"E:\_save2010\mikroC PRO for PIC\uses\P18\" -
SP"C:\_MickroC\_MesProjets\_MCP32424_18f258\" "MCP3424_18F_soft_I2C_LCD_8x16_100909.c"
"__Lib_Math.mcl" "__Lib_MathDouble.mcl" "__Lib_System.mcl" "__Lib_Delays.mcl" "__Lib_CString.mcl"
"__Lib_CStdlib.mcl" "__Lib_Conversions.mcl" "__Lib_SoftI2C.mcl" "__Lib_ADC_A_C.mcl"
"__Lib_EEPROM_256.mcl" "__Lib_UART_c67.mcl"
All files Preprocessed in 50 ms
Compilation Started MCP3424_18F_soft_I2C_LCD_8x16_100909.c
Variable 'HSB_ADC' has been declared, but not used MCP3424_18F_soft_I2C_LCD_8x16_100909.c
Generated baud rate is 56818 bps (error = 1.36 percent) MCP3424_18F_soft_I2C_LCD_8x16_100909.c
Compiled Successfully MCP3424_18F_soft_I2C_LCD_8x16_100909.c
All files Compiled in 821 ms
Used RAM (bytes): 351 (23%)  Free RAM (bytes): 1164 (77%) Used RAM (bytes): 351 (23%)  Free RAM
(bytes): 1164 (77%)
Used ROM (bytes): 4828 (15%)  Free ROM (bytes): 27940 (85%) Used ROM (bytes): 4828 (15%)  Free ROM
(bytes): 27940 (85%)
Project Linked Successfully MCP3424_18F.mcppi
Linked in 1142 ms
Project 'MCP3424_18F.mcppi' completed: 2263 ms
Finished successfully: 09 sept. 2010, 15:03:42 MCP3424_18F.mcppi
*/
// avec PIC18F258  sous MikroC 8.2.0.0
// version sortie LCD & RS232
// version de nokia LCD 8x16 cars
// gestion ADC 18 bits MCP3424 connecté sur port B
// port C    Rc6 Rc7 = UART1
// port B    RB6 RB7 = I2C

// Librairies utilisees   :
//   ADC ,  C_String , Conversions , UART , EEPROM, SoftI2C

/*--- Hardware ------------
PIC 18F258-I/SP DIP20 small
nokia LCD nokia 3310  version 8x16 cars
ADC 18 bits MCP3424 eval board
18-Bit, Multi-Channel Analog-to-Digital Converter with
I2C™ Interface and On-Board Reference
Q=10Mhz
 -----------------------
 PIC    Pin   fonction  pin
RA0     2      EA0
RA1     3      EA1
RA2     4      EA2
RA3     5      EA3
RA4     6
RA5     7
 -----------------------
      PIC      pin      pin LCD Nokia 3310  version 8x16 cars
RC0  SCE        11--R->  5   CS    Chips select
RC1  D_C        12--R->  4   D/C   Data ou Cde
RC2  RES        13--R->  8   RES   reset
RC3  SCK        14--R->  2   SCK   Serial Clock    SCL
RC4  SDI        15--R->  3   DIN   Data output     SDA
                         6   GND    0V
                         7   Capacite 1µF ( 8,2V accross)
                         1   VDD  alim 3,3V not connected !!!
RC5  --->       not used
     Liaison serie           RS232 driver
RC6  Rs_TX      17 ----> 1   DS275 ... ecran
RC7  Rs_Rx      18 <---  3   DS275 ... clavier
---------------------
RB0
RB1
RB2
RB3
RB4
RB5
RB6   ..... I2C  SDA
RB7 .....  I2C SCL
..................
*/

#define A_1 PORTA.F1
#define A_2 PORTA.F2
#define A_3 PORTA.F3
#define A_4 PORTA.F4
#define A_5 PORTA.F5

//--- COM RS232 USART ----
#define Rs_TX PORTC.F6
#define Rs_RX PORTC.F7
/*
#define B_0    PORTB.F0
#define B_2    PORTB.F2
#define B_3    PORTB.F3
#define B_4    PORTB.F4
#define B_5    PORTB.F5
*/

#define B_0    PORTB.F0
#define B_1    PORTB.F1
#define B_2    PORTB.F2
#define B_3    PORTB.F3
#define B_4    PORTB.F4
#define B_5    PORTB.F5
#define B_6    PORTB.F6
#define B_7    PORTB.F7

 //--- LCD nokia sur port C
#define SCE PORTC.F0
#define D_C PORTC.F1
#define RES PORTC.F2
#define SCK PORTC.F3
#define SDI PORTC.F4
//#define notused PORTC.F5
//-------------------------------------
//#define Tracage     // utilise pour debugging seulement
#define TAB 9
#define CR 13
// pour debug partiel
#define LCD_PRESENT   // pour sortie sur LCD (en plus de RS232)
#define EXCEL         // pour sortie avec separateur  virgule

const char table[480] = {
0x00,0x00,0x00,0x00,0x00, // 20 space ASCII table for NOKIA LCD: 96 rows * 5 shorts= 480 shorts
0x00,0x00,0x5f,0x00,0x00, // 21 ! Note that this is the same set of codes for character you
0x00,0x07,0x00,0x07,0x00, // 22 " would find on a HD44780 based character LCD.
0x14,0x7f,0x14,0x7f,0x14, // 23 # Also, given the size of the LCD (84 pixels by 48 pixels),
0x24,0x2a,0x7f,0x2a,0x12, // 24 $ the maximum number of characters per row is only 14.
0x23,0x13,0x08,0x64,0x62, // 25 %  A variant of Nokia 3310 can use 16 char per row and 8 rows
0x36,0x49,0x55,0x22,0x50, // 26 &
0x00,0x05,0x03,0x00,0x00, // 27 '
0x00,0x1c,0x22,0x41,0x00, // 28 (
0x00,0x41,0x22,0x1c,0x00, // 29 )
0x14,0x08,0x3e,0x08,0x14, // 2a *
0x08,0x08,0x3e,0x08,0x08, // 2b +
0x00,0x50,0x30,0x00,0x00, // 2c ,
0x08,0x08,0x08,0x08,0x08, // 2d -
0x00,0x60,0x60,0x00,0x00, // 2e .
0x20,0x10,0x08,0x04,0x02, // 2f /
0x3e,0x51,0x49,0x45,0x3e, // 30 0
0x00,0x42,0x7f,0x40,0x00, // 31 1
0x42,0x61,0x51,0x49,0x46, // 32 2
0x21,0x41,0x45,0x4b,0x31, // 33 3
0x18,0x14,0x12,0x7f,0x10, // 34 4
0x27,0x45,0x45,0x45,0x39, // 35 5
0x3c,0x4a,0x49,0x49,0x30, // 36 6
0x01,0x71,0x09,0x05,0x03, // 37 7
0x36,0x49,0x49,0x49,0x36, // 38 8
0x06,0x49,0x49,0x29,0x1e, // 39 9
0x00,0x36,0x36,0x00,0x00, // 3a :
0x00,0x56,0x36,0x00,0x00, // 3b ;
0x08,0x14,0x22,0x41,0x00, // 3c <
0x14,0x14,0x14,0x14,0x14, // 3d =
0x00,0x41,0x22,0x14,0x08, // 3e >
0x02,0x01,0x51,0x09,0x06, // 3f ?
0x32,0x49,0x79,0x41,0x3e, // 40 @
0x7e,0x11,0x11,0x11,0x7e, // 41 A
0x7f,0x49,0x49,0x49,0x36, // 42 B
0x3e,0x41,0x41,0x41,0x22, // 43 C
0x7f,0x41,0x41,0x22,0x1c, // 44 D
0x7f,0x49,0x49,0x49,0x41, // 45 E
0x7f,0x09,0x09,0x09,0x01, // 46 F
0x3e,0x41,0x49,0x49,0x7a, // 47 G
0x7f,0x08,0x08,0x08,0x7f, // 48 H
0x00,0x41,0x7f,0x41,0x00, // 49 I
0x20,0x40,0x41,0x3f,0x01, // 4a J
0x7f,0x08,0x14,0x22,0x41, // 4b K
0x7f,0x40,0x40,0x40,0x40, // 4c L
0x7f,0x02,0x0c,0x02,0x7f, // 4d M
0x7f,0x04,0x08,0x10,0x7f, // 4e N
0x3e,0x41,0x41,0x41,0x3e, // 4f O
0x7f,0x09,0x09,0x09,0x06, // 50 P
0x3e,0x41,0x51,0x21,0x5e, // 51 Q
0x7f,0x09,0x19,0x29,0x46, // 52 R
0x46,0x49,0x49,0x49,0x31, // 53 S
0x01,0x01,0x7f,0x01,0x01, // 54 T
0x3f,0x40,0x40,0x40,0x3f, // 55 U
0x1f,0x20,0x40,0x20,0x1f, // 56 V
0x3f,0x40,0x38,0x40,0x3f, // 57 W
0x63,0x14,0x08,0x14,0x63, // 58 X
0x07,0x08,0x70,0x08,0x07, // 59 Y
0x61,0x51,0x49,0x45,0x43, // 5a Z
0x00,0x7f,0x41,0x41,0x00, // 5b [
0x02,0x04,0x08,0x10,0x20, // 5c Yen Currency Sign
0x00,0x41,0x41,0x7f,0x00, // 5d ]
0x04,0x02,0x01,0x02,0x04, // 5e ^
0x40,0x40,0x40,0x40,0x40, // 5f _
0x00,0x01,0x02,0x04,0x00, // 60 `
0x20,0x54,0x54,0x54,0x78, // 61 a
0x7f,0x48,0x44,0x44,0x38, // 62 b
0x38,0x44,0x44,0x44,0x20, // 63 c
0x38,0x44,0x44,0x48,0x7f, // 64 d
0x38,0x54,0x54,0x54,0x18, // 65 e
0x08,0x7e,0x09,0x01,0x02, // 66 f
0x0c,0x52,0x52,0x52,0x3e, // 67 g
0x7f,0x08,0x04,0x04,0x78, // 68 h
0x00,0x44,0x7d,0x40,0x00, // 69 i
0x20,0x40,0x44,0x3d,0x00, // 6a j
0x7f,0x10,0x28,0x44,0x00, // 6b k
0x00,0x41,0x7f,0x40,0x00, // 6c l
0x7c,0x04,0x18,0x04,0x78, // 6d m
0x7c,0x08,0x04,0x04,0x78, // 6e n
0x38,0x44,0x44,0x44,0x38, // 6f o
0x7c,0x14,0x14,0x14,0x08, // 70 p
0x08,0x14,0x14,0x18,0x7c, // 71 q
0x7c,0x08,0x04,0x04,0x08, // 72 r
0x48,0x54,0x54,0x54,0x20, // 73 s
0x04,0x3f,0x44,0x40,0x20, // 74 t
0x3c,0x40,0x40,0x20,0x7c, // 75 u
0x1c,0x20,0x40,0x20,0x1c, // 76 v
0x3c,0x40,0x30,0x40,0x3c, // 77 w
0x44,0x28,0x10,0x28,0x44, // 78 x
0x0c,0x50,0x50,0x50,0x3c, // 79 y
0x44,0x64,0x54,0x4c,0x44, // 7a z
0x00,0x08,0x36,0x41,0x00, // 7b <
0x00,0x00,0x7f,0x00,0x00, // 7c |
0x00,0x41,0x36,0x08,0x00, // 7d >
0x10,0x08,0x08,0x10,0x08, // 7e Right Arrow ->
0x78,0x46,0x41,0x46,0x78};  // 7f Left Arrow <-



#define SDA    LATB.F6
#define SCL    LATB.F7
#define LedRouge LATB.F0
// timer1 init à 200mS
#define DUREE_H  0x0B       // 3035\256
#define DUREE_L  0xDB       //3035-(DUREE_H*256)
/* timer1 init 100mS
#define DUREE_H  0x85         // 34285\256
#define DUREE_L  0xED         //34285-(DUREE_H*256)
*/
 //  software_I2c
sbit Soft_I2C_Sda at RB6_bit;  // fil jaune
sbit Soft_I2C_Sda_Direction at TRISB6_bit;
sbit Soft_I2C_Scl at RB7_bit;  // fil vert
sbit Soft_I2C_Scl_Direction at TRISB7_bit;

// ********************* FUNCTIONS ************************
//.. gestion LCD .....
void initlcd(void);   // Initializes the LCD.
void sendcmd(char);   // Writes a command.
void senddata(char);  // Writes data to DDRAM to illuminate the pixels.
void cleanram_8x16(void);  // Erase the DDRAM contents.
void gotoxy(char,char); // Position cursor to x,y.
void sendchar(char);  // Write one character.
void sendpack(char);
//  gestion string
void Affiche_Menu(void);
void Affiche_Menu_RS(void)  ;
void Write_String(void);// envoie une chaine de caracteres
void CRLF(void) ;
// mesures Analog
void   adc_MCP3424_init(void);
unsigned int read_adc_value(void);
void Lectures_ADC10bit(void);

void interrupt (void);

//... declarations de variables communes
char *(txt)="1234567890123456789012345678901234567890";
char *(valtxt)="1234567890123456";
char *mail ="paulfjujo@free.fr";
char tempf[13];
char  car_recu;
float valf;
unsigned int M;
unsigned int N1;
unsigned int M16[8];
short masque;
unsigned int indice;
unsigned int compteur_it;
unsigned short Drapeau;
char outmode;
char dummy;
unsigned int Compteur_Sec;
unsigned short Elligible;
unsigned short MCPCfg;
unsigned short MCP_CH;
unsigned short MCP_Resol;
unsigned short MCP_Gain;

void interrupt ()
{
   if (PIR1.TMR1IF) {
    compteur_it++;
// reinit Timer1 pour 100mS  34285= 85ED
// reinit Timer1 pour 200mS   3035= 0BDB
    TMR1H= DUREE_H;
    TMR1L= DUREE_L;
    PIR1.TMR1IF = 0;
    if (compteur_it>4)       // 0 à 4 =5 5x200mS=1sec
   {
   Compteur_Sec=Compteur_Sec+1;
   compteur_it=0;
   Elligible=1;
   }
      PIR1.TMR1IF = 0;
  }
}

void initlcd()
{
 SCE =1;
 RES =0;
 Delay_ms(200);
 RES=1;            // come out of lcd state
 sendcmd(0x21);    //Function SET: 00100_PD_V_H
 sendcmd(0x90);    //VLCD=;3.06+162*0.06=4.02
 sendcmd(0x05);    //; TC0=1 TC1=0
 sendcmd(0x13);    //bias System 1:48 -> n=4
 sendcmd(0x20);    //Function SET: 00100_PD_V_H
 Delay_ms(100);
 sendcmd(0x0C);
 cleanram_8x16();
 Delay_ms(100);
 gotoxy(0,0);
}

 void sendcmd(char x)
{
 D_C= 0;
 SCE=0;
 sendpack(x);
 Delay_ms(2);
 SCE=1; //deselect the display
}

void senddata(char x)
{
 D_C= 1;
 SCE=0;
 sendpack(x);
 SCE=1; //deselect the display
}

void cleanram_8x16()
{int i;
 gotoxy(0,0);
 for(i=0;i<=768;i++)
 {
 senddata(0x00);
 //Delay_ms(1);
 }
}

void gotoxy( char x, char y)
{
 sendcmd(0x40|(y&0x07));
 sendcmd(0x80|(x&0x7f));
}

void sendchar(char x)
{ int a,b;
 char c;
 if((x<0x20)||(x>0x7f)) return;
 a=((5*x)-160);
 for(b=5;b>0;b--)
  {  c=table[a];
    senddata(c);
    a++;
   }
 senddata(0x00);
}

void sendpack(char x)
{ int i;
 for(i=0;i<8;i++)
 {
  SCK=0;
  if ((x&0x80)==0x80) SDI=1; else SDI=0;
  SCK=1;
  x=x<<1; }
}
//-----------------

void Write_String()
{
 short int i1,j1,k;
 j1=strlen(txt);
   i1=0;
   do
   {
   k=*(txt+i1);
   if (outmode & 0x01)    // RS232 output
       UART1_Write(k);
   else
   sendchar(k);
    i1++;
   }while (i1<j1);
   switch (outmode)
   {
   case 0:   break;
   case 1:   break;
   case 3:   UART1_Write(',');  break;
   case 5:   UART1_Write(9);  break;
   case 7:   UART1_Write(13); UART1_Write(10); break;
   default:  UART1_Write('*'); break;
   }
}

void CRLF()
{
 UART1_Write(10);
 UART1_Write(13);
}

void Lectures_ADC10bit()
{
int number;
for (number=0;number<5;number++)
{
   strcpy(txt,"CH0=");
   *(txt+2)=(short)number+48;
  M = ADC_Read((short)number) ;
  WordToStr(M, valtxt);
  strcat(txt,valtxt);
  Write_String();
    #ifdef EXCEL
   UART1_Write(',');
  #endif
}
}

//--------- ADC 18 bits --------------------
void adc_MCP3424_init()
{
   unsigned short erreur;
   erreur=0;
   Soft_I2C_Start();
   erreur=Soft_I2C_Write(0xD0);
  // ECRITURE adresse fixe + 000 + /W=0
   erreur=Soft_I2C_Write(0x88);
  // RDY=1 init One Shoot conversion
  // MCP_CH * 32  (bit 6-5)  numero de chanel  0 à 3
  // MCP_Resol    (Bits 3-2) 8-> 16bits 15SPS  12->18bits 3,5SPS
  //MCPCfg= MCP_CH << 5;
  //MCPCfg=MCPCfg + 0x80 + MCP_Resol + MCP_Gain;
  //erreur=Soft_I2C_Write(MCPCfg);
   Soft_I2C_Stop();
 }

unsigned int read_adc_value()
{
  unsigned int value;
  unsigned short EtatCf;
  unsigned short HSB_ADC ;
  unsigned short MSB_ADC;
  unsigned short LSB_ADC;
  value=0;
  MCPCfg= 128 + MCP_CH + MCP_Resol + MCP_Gain; // 0x88 = 136
  Soft_I2C_Start();
  Soft_I2C_Write(0xD0);
  Soft_I2C_Write(MCPCfg);
  //Soft_I2C_Write(0x88);
  Soft_I2C_Stop();
  Delay_ms(1);
  Soft_I2C_Start();
  Soft_I2C_Write(0xD1);     // LECTURE adresse fixe + 000 + R=1
  MSB_ADC= Soft_I2C_Read(1);  // lit et renvoi ACK
  LSB_ADC= Soft_I2C_Read(1); // lit et renvoi  ACK
  EtatCf=  Soft_I2C_Read(0); // lit et renvoi NACK
  Soft_I2C_Stop();
  value =  MSB_ADC <<8 ;
  value = value +  LSB_ADC;
  return value;
 }

 void Affiche_Menu(void)
 {
   #ifdef LCD_PRESENT
  gotoxy(0,0);
  #endif
  txt= strcpy(txt,"--ESC = MENU  ");
  Write_String();
  #ifdef LCD_PRESENT
  gotoxy(0,1);
  #endif
  txt= strcpy(txt,"1. Numer Trame ");
  Write_String();
  #ifdef LCD_PRESENT
  gotoxy(0,2);
  #endif
  txt= strcpy(txt,"2. Lect ADC10b ");
  Write_String();
  #ifdef LCD_PRESENT
  gotoxy(0,3);
  #endif                             //12345678901234
  txt= strcpy(txt,"3. Simul 18b   ");
    Write_String();
  #ifdef LCD_PRESENT
    gotoxy(0,4);
  #endif
  txt= strcpy(txt,"+.  GAIN UP    ");
  Write_String();
  #ifdef LCD_PRESENT
    gotoxy(0,5);
  #endif        //12345678901234
 txt= strcpy(txt,"-.  GAIN DOWN  ");
  Write_String();
  }

void main()
{
unsigned int  Nb;
unsigned int i, i1;
  CANCON=0x00;     //   0b 000 0  0000    request disable mode
  CANSTAT =0x00;  //    0b 001 0 000 0    disable mode no interrupt
  TRISB = 0xFC ;  // portb pins 0 et 1 as inputs others as output
  INTCON2.RBPU=1;
  LATB=0xFE;
  PORTA = 0xFF;
  TRISA = 0x01;          // PORTA is RA0=input  EA1..RA5=outputs
  ADCON0 =0x81 ;         // FOSC/32    CH0    AD power on
  ADCON1 = 0x8E ;       //  1=Right justified  0=Fosc/32 0   b1110 =CH0 analo other Digital
  TRISC = 0;            // designate PORTC pins as output
  T0CON=1;
  UART1_Init(19200);    // init RS232   was 57600 mais bug sur 3 premiers caracteres
  if (UART1_Data_Ready() == 1) car_recu=UART1_Read();
  car_recu=0;
  outmode=7 ;    // aiguillage -> RS232 +CR LF
  // presentation
  CRLF();
  txt= strcpy(txt," MCP3424 ADC 16 bits signés 24/10/10");
  Write_String();
 /*
  mail=strcpy(mail,"paulfjujo@free.fr");
  k1=strlen(mail);
  for(i1 = 0; i1 <k1; i1++)   EEPROM_Write(i1,mail[i1]);
 */
  for(i1 = 0; i1 < 17; i1++) {   // Read xx bytes from address 0x00
   i = EEPROM_Read(i1);        //   and send to PC for displaying data
  Delay_ms(50);
   UART1_Write(i);
   }
   CRLF();
   #ifdef LCD_PRESENT
   // -------- PRESENTATION SUR LCD --------
  outmode=0;     // aiguillage -> LCD
  initlcd();
  gotoxy(0,0);
  Affiche_Menu();
  #else
  outmode=5 ;
  #endif
   Delay_ms(1000);
   outmode=1;
   CRLF();
   indice=0;
  car_recu=0;
  compteur_it=0;
  Compteur_Sec=0;
  Nb=0; // compteur de trame
  // valeurs pour simulation ADC
  for (indice=0;indice<4;indice++)
  {
   M16[indice]=indice*1750+525;
   WordToStr(M16[indice], txt);
   Write_String();
   }
  CRLF();
  #ifdef LCD_PRESENT
  outmode=0;     // aiguillage -> LCD
  cleanram_8x16()  ;
  #endif

   Soft_I2C_Init();

   T1CON=0x30;  // mode 8 bits  precaler 1/8  internal clock
   T1CON.TMR1ON=1;  // enable Timer1
   TMR1H = DUREE_L ; //  34285; //  pour 100mS
   TMR1L = DUREE_H ; //  34285;
   PIR1.TMR1IF = 0;
   INTCON.PEIE = 1;
   PIE1.TMR1IE=1;
   INTCON.GIE=1;
   outmode=1;

  Nb=0;
  txt= strcpy(txt,"Config ADC18b=");
  Write_String();

  MCP_CH=0;
  MCP_Resol=8;
  MCP_Gain=0;
  MCPCfg=  0x80 + MCP_CH  + MCP_Resol + MCP_Gain;
//  MCPCfg= 0x88;
  WordToStr(MCPCfg, txt);
  for(i=0;i<5;i++)
   {
    if (*(txt+i)==' ') *(txt+i)='0';
   }
  Write_String();
  CRLF();
  adc_MCP3424_init() ;   // init ADC 18 bits
  Delay_ms(2000);

 // asm CLRWDT;
 Drapeau=2;
 car_recu=0;
 indice=0;
// ***************  main boucle *****************
do
{
  // test si caractere recu dans le buffer UART
  if (UART1_Data_Ready() == 1)
   {
    car_recu=UART1_Read();
    //UART1_Write(car_recu);  // echo
    switch (car_recu)
     {
      case '1': Drapeau = Drapeau ^ 1 ; break;
      case '2': Drapeau = Drapeau ^ 2 ; break;
      case '3': Drapeau = Drapeau ^ 4 ; break;
      case '+':
               if (MCP_Gain < 3) MCP_Gain = MCP_Gain + 1;
               break;
      case '-':
              if (MCP_Gain > 0) MCP_Gain = MCP_Gain -1;
              break;
      case 27 :
                CRLF();
                 Affiche_Menu();
                CRLF();
                 break;
      default:   break;
      }
  }
  Nb=Nb+1;
  outmode=1;       // RS232 select
  if (Drapeau & 0x01)
  {
  txt= strcpy(txt,"Nb=");
  WordToStr(Nb, valtxt);
  for(i=0;i<5;i++)
  {
    if (*(valtxt+i)==' ') *(valtxt+i)='0';
  }
  txt=strcat(txt,valtxt);
  Write_String();
  #ifdef EXCEL
   UART1_Write(9);
  #endif
  }
  if (Drapeau & 0x02)
  {
  Lectures_ADC10bit();
  }
  UART1_Write(9);
  #ifdef LCD_PRESENT
   gotoxy(0,0);
  #endif
  indice=0;
  do
  {
  // Attention ORDRE ZARBI des channels ADC
  // MCP_CH=indice << 5  ; // was * 32
    switch (indice)
     {
      case 0 : MCP_CH=32 ; break;
      case 1 : MCP_CH=64 ; break;
      case 2 : MCP_CH=96 ; break;
      case 3 : MCP_CH=0 ; break;
        default:   break;
      }

   if (Drapeau & 0x04)
       M = M16[indice];
       else
          M=read_adc_value();
    WordToStr( M, txt);   // mesure transformée binaire -> text ascii
   // formatage sortie numerique sur 5 cars avec "0" en tete
   for(i=0;i<5;i++)
   {
    if (*(txt+i)==' ') *(txt+i)='0';
   }
   outmode=1;            // ecriture sur sortie RS232
   Write_String();
  #ifdef EXCEL
   // rajout separateur , sauf sur derniere mesure
   // pour exploiter les mesures sous excel
   if (indice<3)
   {
     UART1_Write(',');
   }
  #endif
  #ifdef LCD_PRESENT
    outmode=0;
    gotoxy(0,0);  //1234567890123456
    txt=strcpy(txt,"VERSION 10/09/10");
    Write_String();
    gotoxy(0,1);  //1234567890123456
    txt=strcpy(txt," MCP3424 15bits  ");
    Write_String();
    gotoxy(0,2);
    txt=strcpy(txt," 16 bits signes ");
    Write_String();
  //  WordToStr(Compteur_Sec,txt);   //  Bugg!!!!!
  //  gotoxy(42,2);
  //  Write_String();

    gotoxy(0,3);
    txt=strcpy(txt,"Gain=");
    WordToStr(MCP_Gain,valtxt);
    txt=strcat(txt,valtxt);
    Write_String();
    gotoxy(0,indice+4);
    txt=strcpy(txt,"CH.=");
    *(txt+2)=(short)indice +49; // CH1 à CH4 pour indice 00 à 03
    Write_String();
    gotoxy(28,indice+4);
    WordToStr(M, txt);
    Write_String();
    outmode=1;
  #endif
   indice ++;
  } while (indice < 4);

   UART1_Write(',');
   WordToStr(MCP_Gain,txt);
   Write_String();
   UART1_Write(',');

 /* valf=3.14159;
  FloatToStr(valf,tempf);
   Write_String();
 */
   LedRouge=! LedRouge;
   outmode=1;
   Drapeau=Drapeau & 0x07;
  CRLF();

  // attente synchro sur seconde (It Timer1)
  do
  {}
  while(Elligible==0);
  Elligible=0;
  }
  while(1);
 }
/*
28/08/2010 TEST MCP3424 Eval Board
source de tension de test = pot 10 tours alimenté par le +5V d'alim carte
mode Single Ended donc   CH1- au 0V  Input sur CH1+
GAIN=1  à 8    16 bits signes => 15 bits utiles en mode single end
*/




Post Reply

Return to “mikroC General”