Version:0.9 StartHTML:0000000105 EndHTML:0000094057 StartFragment:0000001167 EndFragment:0000094041 mikroElektonika
/*Connected to \\.\COM2 at 19200
  HEX: 2 min old, INHX32,18Fcode+cfg, total=3470 bytes.
  Searching for PIC ...
  Found:18F 252o/452o
  WRITE OK  at 10:04,   time:3.364 sec
*/
// Rajout gestion ADC 16 bits AD7715  connecté sur port B
// affichage value sur LCD ET RS2232
// usage LCD nokia 3310 avec PIC18F252  sous MikroC 8.2.0.0
// hardware LCD connecté sur PORT C
// modif clear LCD , car "bavures"
// voir Nokia_Xport_09090.asm pour details cdes LCD

/*--- Hardware ------------
PIC 18F252 DIP20 small
ADC 16 bits  DIP16
Q=10Mhz
 -----------------------
      PIC      pin      pin LCD Nokia 3310
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
RC4  SDI        15--R->  3   DIN   Data Input
                         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      26 ----> 1   DS275 ... ecran
RC7  Rs_Rx      27 <---  3   DS275 ... clavier
 ---------------------
     PIC      pin      pin  AD7715
RB0  ADC_DI     21 ---> 14   DIN
RB1  ADC_DRDYI  22 <--- 12   /DRDY
RB2  ADC_CS     23 --->  4   /CS
RB3  ADC_RESET  24 --->  5   /RESET
RB4  ADC_DO     25 ---> 13   DOUT
RB5  ADC_CLK    26 --->  1   SCLK
RB6  --->        not used
RB7  ---Led rouge -- 560 ohms --< +5V
...................Other pins AD7715
      7 +AIN input
      8 -AIN input =0V
     11 AGND  =0V
     16 DGND  =0V
     15 AVDD  =+5V
      6 DVDD  =+5V
      2 Quartz 1MHz
      3 Quartz 1MHz
AD580 reference 2,50V  ajustement via potar à 2,048V
      1 +5V
      2 sortie 2,5V
      3 0V
*/
 // 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
// ---- RS232 USART ----
#define Rs_TX PORTC.F6  // pin 26
#define Rs_RX PORTC.F7  // pin 27
// --- definitions pour AD7715 -----------------------
#define ADC_DI    PORTB.F0
#define ADC_DRDY  PORTB.F1
#define ADC_CS    PORTB.F2
#define ADC_RESET PORTB.F3
#define ADC_DO    PORTB.F4
#define ADC_CLK   PORTB.F5
//#define notused PORTB.F6
#define LedRouge  PORTB.F7
//-------------------------------------------------------
#define pi 3.14159 // Constant pi. 
#define num_of_points 64 // Number of points to plot in a graph. 
//#define Tracage            // utilise pour debugging seulement

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(void);  // Erase the DDRAM contents.
void gotoxy(char,char); // Position cursor to x,y. 
void sendchar(char);  // Write one character.
void sendpack(char);
void Write_String(void);// envoie une chaine de caracteres
//void CRLF(void) ;

void write_adc_short(short);
void adc_7715_init(void);
long int read_adc_word(void) ;
long int read_adc_value(void);

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 % 
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 <-

char *txt="1234567890123456.";
char *valtxt="123456";
unsigned int M;
char outmode=0;       // 0=LCD 1=RS232

void main()
{
int i ;
short Nb;
  PORTB = 0x00;
  TRISB = 0b00010010 ;  // portb pins 0 et 1 as inputs others as output
  INTCON2.RBPU=0;
  PORTA = 0xFF;
  TRISA = 0xFF;          // PORTA is RA0,1,2,3,4,5=input
  ADCON0 =1 ;
 // portA all analog , An3=+ref=5V
  ADCON1 = 0x81 ;       //b  10000001
  TRISC = 0;            // designate PORTC pins as output
   USART_Init(56000);
  LedRouge=0;
  initlcd();
  Delay_ms(100);
  gotoxy(0,0);
  txt= strcpy(txt,"Nokia_LCD_18F252");
  Write_String();
  outmode=0;
  Nb=0;
  adc_7715_init();
   
 while(1)
 {

   gotoxy(0,1);
  // Calibre pour txt LCD
  //              "1234567890123456"
  txt= strcpy(txt,"Versus 03 mai 10");
  Write_String();

  txt= strcpy(txt,"ADC CH0=");
  gotoxy(0,2);
  M = ADC_Read(0) ;
  WordToStr(M, valtxt);
  strcat(txt,valtxt);
  Write_String();
  outmode=1;  Write_String(); outmode=0;

  txt= strcpy(txt,"ADC CH1=");
  gotoxy(0,3);
  M = ADC_Read(1) ;
  WordToStr(M, valtxt);
  strcat(txt,valtxt);
  Write_String();
  outmode=1;  Write_String(); outmode=0;

   //adc_7715_init();

  Nb=Nb+1;
  if(Nb==0)
  {
     adc_7715_init();
     Nb=0;
   }
   M=Nb & 0x00FF;
   WordToStr(M, txt);
   gotoxy(0,4);
   Write_String();
     
  gotoxy(0,5);
  txt= strcpy(txt,"Value =");
  M=read_adc_value() ;
  WordToStr(M, valtxt);
  strcat(txt,valtxt);
  Write_String();
  outmode=1;  Write_String(); outmode=0;
  
  gotoxy(0,7);
  txt= strcpy(txt,"Paulfjujo@freefr");
  Write_String();
  if(LedRouge==0)
   LedRouge=1;
  else LedRouge=0;
  Delay_ms(250);
 }
}

void initlcd() 
{ 
 RES =1;
 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(10);
 sendcmd(0x0C);
 cleanram();
 Delay_ms(100);
 gotoxy(0,0);
} 

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

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


void cleanram() 
{int i; 
 gotoxy(0,0);
 for(i=0;i<=896;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==1)
      Usart_Write(k);
   else
   sendchar(k);
    i1++;
   }while (i1<j1);
   if (outmode==1)
   {
    Usart_Write(10);
    Usart_Write(13);
   }
}
//--------- ADC 16 bits --------------------

void adc_7715_init()
{  int i;
	ADC_RESET=0;
  ADC_CLK=1;
	ADC_CS=1;	  //Select AD7715
	ADC_RESET=1;	//Reset AD7715
	ADC_DI=1;
	ADC_CLK=1;
	for (i=1;i<=5;i++)
	{
    write_adc_short(0x10)  ;
    write_adc_short(0xFF) ;
  }
  write_adc_short(0x10)  ;
  write_adc_short(0x00) ;
	delay_ms(250);
	//Communications Register :  acces au setup , prochain acces en ecriture ,
	// pas de STBY,   GAIN=1
	write_adc_short(0x10);
	write_adc_short(0x44);
		delay_ms(250);
  //Setup Register: Mode SelfCalibration automatique , Clk=1MHz,
  //filtre=50Hz ,Unipolar ,Buffer ,Pas de synchro
}

void write_adc_short(short data)
 {
   short i,k;
   ADC_CS=0;
   k=0;
   for(i=1;i<=8;++i)
   {
      ADC_CLK=0;
     if ((data & 0x80)==0x80)
     {
      ADC_DI=1;
     #ifdef Tracage
      Usart_Write(49);
      #endif
      }
      else
      {
      ADC_DI=0;
       #ifdef Tracage
        Usart_Write(48);
        #endif
      }
      data=data<<1;
      ADC_CLK=1;
    }
   ADC_CS=1;
   ADC_CLK=1;
  #ifdef Tracage
   Usart_Write(13) ;
   Usart_Write(10) ;
  #endif
}

long int read_adc_word() {
   short i;
   long data;
   ADC_CS=0;
   data=0;
     for(i=15;i>=0;--i) {
      ADC_CLK=0;
      ADC_CLK=1;
      if (ADC_DO==1) data=data+ (1 << (i-1)) ;
   }
   ADC_CS=1;
   return data;
}

long int read_adc_value()
{
  long int value;
  while ( ADC_DRDY==1) ;
	while ( ADC_DRDY==0) ;
  while ( ADC_DRDY==1) ;
  // DRDY=0 ZERo=0 RS1=1 RS0=1 R/W=1  STBY=0  G1=0 G0=0
  //  operation  d'ecriture dans le  registre de communication
  // avec Gain=1
  //  le prochain acces sera en Lecture sur le Data Register
  write_adc_short(0x38); // Gain=1  32767 points pour 2,048V
 // write_adc_short(0x39); // Gain=2    32767 points pour 1,024V
  value=read_adc_word();
  return value;
}

/*
ADC CH0=  958
ADC CH1=  164
Value = 3200             <-  200,1mV
.....
ADC CH0=  958
ADC CH1=  164
Value =    2            <-  0,35mV
  */