DE PIECES
Acceuil Fonctions utilisées
Les fonctions utilisées avec explication.
1_Configuration du E/S.
2_Fonction du communication I2C avec Microcontrôleur.
3_Fonction de l’horloge PCF8583?.
4_Fonction d'afficher l'horloge sur le LCD
5_Fonction de calcul pour les 3 compteurs
6_Fonction d'incrémentation des 3 compteurs utilisés
7_Fonction d'affichage du compteurs
8_Fonction d'enregistrement pour la mémoire EEPROM
9_Fonction d’initialisation pour la communication série avec le PC
10_Fonction de l’émission et l'affichage sur PC
Le programme.
1
#define sdaon TRISC.F4=0;PORTC.F4=1;delay_us(1);#define sclon TRISC.F3=0;PORTC.F3=1;delay_us(1);
#define sdaoff TRISC.F4=0;PORTC.F4=0;delay_us(1);
#define scloff TRISC.F3=0;PORTC.F3=0;delay_us(1);
unsigned char val,co,l,cpt1=0,cpt2=0,cpt3=0,n=0;
unsigned char se,h,m,i=0,j,mois,date,K;
char t[7][10]={"DIM","LUN","MAR","MER","JEU","VEN","SAM"};
==>==>==>==>==>==>==>==>==>==>==>==>==>
2
void start(){
sdaon;
sclon;
sdaoff;
scloff;
}
void stop()
{
scloff;
sdaoff;
sclon;
sdaon;
}
void ackesclave()
{
unsigned char A;
scloff;
sdaon;
sclon;
A=PORTC.F4;
if(A==0) scloff;
}
void transmission(unsigned char d)
{
unsigned char M=0X80;
int i;
for(i=0;i<8;i++)
{
if((d&M)!=0)
{sdaon;}
else
{sdaoff; }
sclon;scloff;
M=M>>1;
}
}
unsigned char reception()
{
unsigned char v=0x00,m=0x80;
int i;
sdaon;
for(i=0;i<8;i++)
{
sclon;
TRISC.F4=1;
if((PORTC.F4)==1)
v=v|m;
scloff;
m=m>>1;
}
return (v);
}
==>==>==>==>==>==>==>==>==>==>==>==>
3
void ecrirehorloge(unsigned char adrm,unsigned char d){
start();
transmission(0xA0);
ackesclave();
transmission(adrm);
ackesclave();
transmission(d);
ackesclave();
stop();
}
unsigned char lire_horloge(unsigned char adresse)
{
unsigned char donnee;
I2C_Start();
I2C_Wr(0xA0);
I2C_Wr(adresse);
I2C_Repeated_Start();
I2C_Wr(0xA1);
donnee = I2C_Rd(0u);
I2C_Stop();
return donnee;
delay_ms(100);
}
==>==>==>==>==>==>==>==>==>==>==>
4
void pcf(){ se=lire_horloge(0x02);
m=lire_horloge(0x03);
h=lire_horloge(0x04);
j=lire_horloge(0x06)>>5;
mois=lire_horloge(0x06)& 0x1F;
date=lire_horloge(0x05)& 0x3F;
Lcd_Out(1,1, t[j]);
Lcd_Chr(1,5,(date>>4)+48);
Lcd_Chr(1,6,(date&0x0F)+48);
Lcd_Out(1,7, "/");
Lcd_Chr(1,8,(mois>>4)+48);
Lcd_Chr(1,9,(mois&0x0F)+48);
Lcd_Chr(1,12,(h>>4)+48);
Lcd_Chr(1,13,(h&0x0F)+48);
Lcd_Out(1,14, ":");
Lcd_Chr(1,15,(m>>4)+48);
Lcd_Chr(1,16,(m&0x0F)+48);
}
==>==>==>==>==>==>==>==>==>==>==>
5
void affiche(unsigned char Val,unsigned char l,unsigned char co){
val= Val%10;
Lcd_Chr(l,co, val+48);
Val=Val/10;
val= Val%10;
Lcd_Chr(l,co-1, val+48);
Val=Val/10;
val= Val%10;
Lcd_Chr(l,co-2, val+48);
Val=Val/10;
val= Val%10;
Lcd_Chr(l,co-3, val+48);
}
<><><><><><><><><><><><><><>
6
unsigned char compteur(){
if(PORTC.F0==0)
{cpt1++;delay_ms(150);}
if(PORTC.F1==0)
{cpt2++;delay_ms(150);}
if(PORTC.F2==0)
{cpt3++;delay_ms(150);}
return cpt1,cpt2,cpt3;
}
==>==>==>==>==>==>==>==>==>
7
void afficher_cpt(){
if(PORTD.F0==0)
{
Lcd_Out(2,1, "Compt1 :");
affiche(cpt1,2,12);
}
if(PORTD.F1==0)
{
Lcd_Out(2,1, "Compt2 :");
affiche(cpt2,2,12);
}
if(PORTD.F2==0)
{
Lcd_Out(2,1, "Compt3 :");
affiche(cpt3,2,12);
}
}
==>==>==>==>==>==>==>==>==>
8
void interrupt(){
if (PIR1.TMR1IF==1)
{
TMR1L=0XB0;
TMR1H=0X3C;
n++;
if(n==36000) //30 minutes*60 secondes*20
{
n=0;
PORTB.F7=!PORTB.F7;
EEprom_Write(0,date+48);
EEprom_Write(1,mois+48);
EEprom_Write(2,h+48);
EEprom_Write(3,m+48);
EEprom_Write(4,cpt1);
EEprom_Write(5,cpt2);
EEprom_Write(6,cpt3);
}
PIR1.TMR1IF=0;
}
if(PIR1.RCIF==1) // indique reception d'un octet sur USART
{
K = RCREG;
PIR1.RCIF=0;
}
}
==>==>==>==>==>==>==>==>==>
9
void Serial_Init(void) //initialisation Usart{
SPBRG = 12 ;
TXSTA = 0b00100000 ; //TXEN=1 autorise émission de donnée
RCSTA = 0b10010000 ; //SPEN=1 valide le port serie ; CREN valide reception
}
<><><><><><><><><><><><><>
10
void Serial_Putchar(unsigned char V) //emission d'un caractere{
while(TXSTA.TRMT==0); // boucle d'attente registre d'emission vide
TXREG = V; //placement du caractere a envoyer
}
<==><==><==><==><==><==><==>
La fonction globale.
void main()
{
ecrirehorloge(0x02,0x00); ecrirehorloge(0x05,0xA5);
ecrirehorloge(0x03,0x30); ecrirehorloge(0x06,0x21);
ecrirehorloge(0x04,0x16);
ANSEL=0x00; //travailler en mode numerique
ANSELH=0x00;
TRISB=0x00; PORTB=0X00;
TRISC=0XFF; PORTC=0XFF;
TRISD=0XFF; PORTD=0XFF;
Serial_Init();
I2C_Init(100000);
INTCON.GIE=1;
INTCON.PEIE=1;
PIE1.TMR1IF=1;
TMR1L=0XB0;
TMR1H=0X3C;
T1CON.TMR1CS=0;
T1CON.T1CKPS0=1;
T1CON.T1CKPS1=0;
T1CON.TMR1ON=1;
Lcd_Config(&PORTB, 4, 5, 6, 3, 2, 1, 0);
Lcd_Cmd(LCD_CURSOR_OFF);
Lcd_Cmd(Lcd_Clear);
while(1)
{
pcf();
compteur();
afficher_cpt();
if((h==0x18)&&(se==0x00))
{
cpt1=0;cpt2=0;cpt3=0;
//PORTB.F7=1;
}
if(K=='?')
{
for(l=0;l<10;l++)
Serial_Putchar(Eeprom_Read(l));
}
}
}