' ' Controle du déplacement du sumotozor ' Le récepteur radio 41Mhz est branché sur A6 et A7 ' Réalisation : GROUPE EXPOSCIENCE DU LYCEE LOUIS PAYEN ' ( élèves de 1ère SSI - Sciences de l'ingénieur 2005-2006 ) ' Utilisation du Timer pour variation de vitesse par MLI ' ' Utilisation du Timer ' Clignoter led ROUGE à basse fréquence sous interruption ' Pour modifier le timer (CboyF1 16mhz): ' PACTTL.1 PACTL.0 ' 0 0 2,05 ms ' 0 1 4,1 ms ' 1 0 8,2 ms ' 1 1 16,4 ms #include "startcf1.bas" byte t, passage, flag, n int compteur, compteur2, v ' Configuration DDRD = 0 ' Port D en entrée DDRA = %00111111 ' A0 à A5 en sortie A6,A7 en entrée PACTL.1 = 0 ' selectionner la vitesse (2ms sur cboyF1 16Mhz) PACTL.0 = 0 ' en fonction de ce qu'on souhaite ' Voir tableau en haut du programme TMSK2.6 = 1 ' declencher le timer cli ' autoriser les inter lcdinit() ' initialisation passage = 0 flag = 0 ' pour toujours do do compteur = 0 ' compteur voie 1 compteur2 = 0 ' compteur voie 2 do loop until PORTA.7 = 0 ' attente niveau 0 do loop until PORTA.7 = 1 ' Détection du front montant sur A7 do compteur = compteur +1 loop until PORTA.7 = 0 ' faire de même pour compteur2 sur a6 do loop until PORTA.6 = 1 ' Détection du front montant sur A6 do compteur2 = compteur2 +1 loop until PORTA.6 = 0 loop until compteur>180 AND compteur<320 AND compteur2>180 AND compteur2<320 ' print "#1 compt1 = ",compteur ' print "#2 compt2 = ",compteur2 ' tempo(50) ' on temporise moins d'une seconde ' Tester ensuite valeurs des compteurs et ' agir sur le véhicule if compteur >= 240 and compteur <= 270 then ' Neutre gauche/droite gosub vitesse if compteur2 < 245 then gosub avancer end if if compteur2 >= 245 and compteur2 <= 265 then gosub arret end if if compteur2 > 265 then gosub reculer end if end if if compteur2 >= 250 and compteur2 <= 260 then ' Neutre avancer/reculer gosub vitesse2 if compteur > 265 then gosub droite end if if compteur < 245 then gosub gauche end if end if loop vitesse: ' calcul de la vitesse ' 188 min avance 255 neutre 315 max recul if compteur2 > 255 then ' pour valeur absolue v = compteur2 - 255 else v = 255 - compteur2 end if n = v/3 return vitesse2: ' calcul de la vitesse ' 188 min gauche droite 255 neutre 315 max (à vérifier) if compteur > 255 then ' pour valeur absolue v = compteur - 255 else v = 255 - compteur end if n = v/3 return reculer: ' MOTEUR 1 PORTA.1 = 1 PORTA.2 = 0 ' MOTEUR 2 PORTA.4 = 1 PORTA.5 = 0 tempo (100) return avancer: PORTA.1 = 0 PORTA.2 = 1 PORTA.4 = 0 PORTA.5 = 1 tempo(100) return arret: PORTA.0 = 1 PORTA.1 = 0 PORTA.2 = 0 PORTA.3 = 1 PORTA.4 = 0 PORTA.5 = 0 return gauche: PORTA.1 = 1 PORTA.2 = 0 PORTA.4 = 0 PORTA.5 = 1 tempo(25) return droite: PORTA.1 = 0 PORTA.2 = 1 PORTA.4 = 1 PORTA.5 = 0 tempo(25) return ' Fonction interruption du TIMER : interrupt function rtiint at $FFF0 passage=passage+1 if passage 20 then passage=0 end if TFLG2.6 = 1 ' autoriser ints a nouveau end function function tempo(cnt) int i, k for cnt=cnt to 0 step -1 for i=0 to 100 next next return 0 end function //********* driver LCD Good **************************************** #define LCDLINES 4 //+++ lignes/afficheur: 1,2,3,4 #define LCDCOLS 16 //+++ caracters par lignes 16,20 #define LCDBUFSZ (LCDLINES * LCDCOLS) byte lcdbuf(LCDBUFSZ) byte lcdbufpp byte code_lcdpos()=00,00,40,20,60 //+++ pos de lignes dans la mémoire LCD byte code_lcdlng()=80,20,20,20,20 //+++ longeur de lignes dans la mém LCD function lcdnib(nibc) // Controlboy F1 nibc = (nibc AND 0x4F) OR 0x30 PORTM = nibc PORTM = nibc OR 0x80 ' EN = 1 500 ns PORTM = nibc end function function lcdnibw(lcdc) lcdnib(lcdc) lcdtempo(-1) end function function lcdputc(lcdc) lcdnib(lcdc/16) lcdnibw(lcdc AND 0xF) end function function lcdinit() byte c PORTB = 0x30 lcdtempo(5) ' 15 ms lcdnib(3) ' lcd = 3 lcdtempo(1) ' 4.1 ms lcdnib(3) ' lcd = 3 lcdtempo(1) ' .1 ms lcdnibw(3) ' lcd = 3 40us lcdnibw(2) ' lcd = 2 40us lcdputc(0x28) lcdputc(0x08) ' display off lcdputc(0x01) ' clear display lcdtempo(1) lcdputc(0x06) lcdputc(0x0E) ' display on , cursor for c=0 to (LCDBUFSZ-1) lcdbuf(c) = ` ` next c sendlcdbuf() end function function putchar(lcdc) byte put_x if (lcdc==`#`) then lcdbufpp=255 return end if if (lcdbufpp=255) then if (lcdc==`*`) then //+rs232+ lcdbufpp=254 //+rs232+ return //+rs232+ end if //+rs232+ put_x = lcdc AND 7 lcdbufpp = code_lcdpos(put_x) put_x = code_lcdlng(put_x) for lcdc=lcdbufpp to lcdbufpp+put_x-1 lcdbuf(lcdc) = ` ` next lcdc goto sendnow; end if if (lcdbufpp==254) then //+rs232+ do //+rs232+ loop until SCSR.7=1 //+rs232+ SCDR=lcdc //+rs232+ return //+rs232+ end if //+rs232+ if (lcdbufpp>=LCDBUFSZ) then lcdbufpp=0; end if lcdbuf(lcdbufpp) = lcdc lcdbufpp = lcdbufpp+1 sendnow: sendlcdbuf() end function function sendlcdbuf() byte d lcdputc(0x80) for d=0 to LCDBUFSZ-1 ' if d = (LCDBUFSZ/2) then //+++ specific afficheur ' lcdputc(0x80 + 0x28) //+++ specific afficheur ' end if lcdnib ((lcdbuf(d)/16) OR 0x40) lcdnibw (lcdbuf(d) OR 0x40) next d end function function lcdtempo(cnt) int bcl for cnt=cnt to 0 step -1 for bcl=0 to 100 next next end function byte old_keycode function keyget() byte keycode keycode = 0 PORTM = 0x37 ' K=0 J=1 H=1 G=1 if PORTN.3 = 0 then keycode=`1` if PORTN.2 = 0 then keycode=`2` if PORTN.1 = 0 then keycode=`3` if PORTN.0 = 0 then keycode=`?` PORTM = 0x3B ' K=1 J=0 H=1 G=1 if PORTN.3 = 0 then keycode=`4` if PORTN.2 = 0 then keycode=`5` if PORTN.1 = 0 then keycode=`6` if PORTN.0 = 0 then keycode=`?` PORTM = 0x3D ' K=1 J=1 H=0 G=1 if PORTN.3 = 0 then keycode=`7` if PORTN.2 = 0 then keycode=`8` if PORTN.1 = 0 then keycode=`9` if PORTN.0 = 0 then keycode=`?` PORTM = 0x3E ' K=1 J=1 H=1 G=0 if PORTN.3 = 0 then keycode=`*` if PORTN.2 = 0 then keycode=`0` if PORTN.1 = 0 then keycode=`#` if PORTN.0 = 0 then keycode=`?` if keycode==old_keycode then return 0 old_keycode = keycode return keycode end function