PROGRAMME POUR LES SERVOMOTEURS DU 68HC11BUG










' Programme robot 1
' 18-12-01
'
' Ne pas oublier de placer un "wai' àpres la configuration de servos,
'il risque sinon d'y avoir des pertes d'impulsions
'
#include "startcf1.bas"
    byte t(8), i, pos, comp, ordre, tabmsk(8), tabmsk2(8), msk, msk2,c

    lcdinit()        ' enable l'afficheur
    PRINT "Controleur de servos"
   
    PACTL.4 = 0       
    PACTL.5 = 1
PACTL.6 = 1 ' PAEN à 1 pour faire travailler A7 ...
    DDRA=127        ' PA0 à PA6 en sorties, PA7 en entrée
    TMSK2.1 = 0
    TMSK2.4 = 1        ' declencher les int sur PA7   
    TMSK2.6 = 0
    TFLG2.6 = 0



t(7)= 0
   
    tabmsk(0)= 0
    tabmsk(1)= 1
    tabmsk(2)= 2
    tabmsk(3)= 4
    tabmsk(4)= 8
    tabmsk(5)= 16
    tabmsk(6)= 32
    tabmsk(7)= 64

 
  tabmsk2(0)=%11111111
    tabmsk2(1)=%11111110
    tabmsk2(2)=%11111101
    tabmsk2(3)=%11111011
    tabmsk2(4)=%11110111
    tabmsk2(5)=%11101111

    tabmsk2(6)=%11011111
    tabmsk2(7)=%10111111

    for i = 0 to 5
    conf_servo(i,4)
    next i


    cli

    do   


    PACNT = 0
    wai
    comp = PACNT

' interpretation de la valeur du comptage
' on utilise les encadrements


    if comp < 70 then
        ordre = 0
        goto fin
    end if

    if comp < 80 then
        ordre = 1
        goto fin
    end if

    if comp < 90 then
        ordre = 2
        goto fin
    end if

    if comp < 100 then
        ordre = 3
        goto fin
    end if

    if comp < 110 then
        ordre = 4
        goto fin
    end if

    if comp < 120 then
        ordre = 5
        goto fin
    end if
   
    if comp < 130 then
        ordre = 6
        goto fin
    end if

    if comp < 140 then
        ordre = 7
        goto fin
    end if

fin:

' affichage de l'ordre reçu

    if comp>99 then
        PRINT " comp= ", comp," "
        PRINT " ordre = ",ordre," "
    else
        print " comp= ",comp," "
        PRINT " ordre = ",ordre," "
    end if
    lcdtempo(100)

' on place ici l'interprétation des ordres
    if ordre = 1 then
        gosub av_gauche
        gosub av_droite
    end if
    wai

    if ordre = 2 then
        gosub av_gauche
    end if
    wai

    if ordre = 3 then
    gosub av_droite
    end if
    wai


    loop                  'on boucle


av_gauche:                  ' avancer la partie gauce du robot
    conf_servo(2,5)             ' on descend la patte 2
    lcdtempo(300)
    conf_servo(3,0)             ' on fait avancer les pattes 1 et 3
    conf_servo(1,1)
    lcdtempo(300)
    conf_servo(2,2)             ' on descend le robot
    lcdtempo(300)
    conf_servo(1,4)             ' et on tire pour avancer
    conf_servo(3,3)
    return


av_droite:                 ' avancer la partie droite du robot
    conf_servo(5,0)             ' même chose, mais pour la partie droite
    lcdtempo(300)
    conf_servo(6,7)
    conf_servo(4,4)
    lcdtempo(300)
    conf_servo(5,3)
    lcdtempo(300)
    conf_servo(4,0)
    conf_servo(6,2)
    return
   
   


function conf_servo(numero,position)
    msk=tabmsk(numero)
    msk2=tabmsk2(numero)

    for i=0 to position
    t(i)=t(i) or msk
    next i
    for i = position to 7
    t(i)=t(i) and msk2
    next i
    wai
    end function



interrupt function rtiint at $FFDA
' mesure de l'impulsion
'    comp = 0
'    do
'    comp = comp + 1
'    loop until PORTA.7 = 0

' lecture du tableau
    PORTA=255
    pshy                 'tempo de 0.8ms, impulsion minimale à envoyer
    ldy #400             ' sauvgarde du registre y, on charge dans y la valeur 270
_tmp:    nop                 ' nop: 2 cycles machine à ne rien faire
    dey                 ' decrémentation du registre y
    bne _tmp             ' retourne à "_tmp" si y est positif
    puly                 ' fin de tempo, on recharge la valeur de y
   


    for i= 0 to 6              ' boucle de lecture du tableau

    PORTA = t(i)

    pshy                 ' tempo entre chaque colonne
    ldy #100
_tmp2:    nop
    dey
    bne _tmp2
    puly           

    next i
PORTA = 0

    TFLG2 = $10 ' autoriser ints a nouveau
    end function



'******************************* lcd *****************************************

' PORTM bit7=LCD EN = 0 by default
' PORTM bit4=DAC CS = 1 by default

byte lcdbuf(32)

function lcdnib(nibc)
    nibc = (nibc AND 0x4F) OR 0x30
    PORTM = nibc
    PORTM = nibc OR 0x80    ' EN = 1 500 ns
    PORTM = nibc
    end function


function lcdnibw(nibc)
    lcdnib(nibc)
    lcdtempo(-1)
    end function

function lcdputc(byte_c)
    lcdnib(byte_c/16)
    lcdnibw(byte_c AND 0xF)
    end function

function lcdinit()
    byte init_c
    PORTM = 0x3F
    lcdtempo(5)        ' 15 ms
    lcdnib(3)        ' lcd = 3
    lcdtempo(1)        ' 4.1 ms
    lcdnib(3)        ' lcd = 3
    lcdtempo(0)        ' .1 ms
    lcdnibw(3)        ' lcd = 3 40us
    lcdnibw(2)        ' lcd = 2 40us
    lcdputc(0x29)
    lcdputc(0x08)        ' display off
    lcdputc(0x01)        ' clear display
    lcdtempo(1)
    lcdputc(0x06)
    lcdputc(0x0E)        ' display on , cursor
    for init_c=0 to 31
        lcdbuf(init_c) = ` `
    next init_c
    putchar(` `)
    end function

function putchar(ascii_char)
    byte put_cnt
    for put_cnt=0 to 30
        lcdbuf(put_cnt) = lcdbuf(put_cnt+1)
    next put_cnt
    lcdbuf(31) = ascii_char
    lcdputc(0x80)
    for put_cnt=0 to 31
        if put_cnt = 16 then lcdputc(0x80+0x28)
        lcdnib ((lcdbuf(put_cnt)/16) OR 0x40)
        lcdnibw (lcdbuf(put_cnt) OR 0x40)
    next put_cnt
    end function

function lcdtempo(tempo_cnt)
    int tempo_i
    for tempo_cnt=tempo_cnt to 0 step -1
        for tempo_i=0 to 200
        next
    next
    end function

'******************************* keyboard ************************************

    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=`C`
    if PORTN.2 = 0 then keycode=`B`
    if PORTN.1 = 0 then keycode=`0`
if PORTN.0 = 0 then keycode=`A`
    PORTM = 0x3B                 ' K=1 J=0 H=1 G=1
    if PORTN.3 = 0 then keycode=`D`
    if PORTN.2 = 0 then keycode=`9`
    if PORTN.1 = 0 then keycode=`8`
    if PORTN.0 = 0 then keycode=`7`
    PORTM = 0x3D                 ' K=1 J=1 H=0 G=1
    if PORTN.3 = 0 then keycode=`E`
    if PORTN.2 = 0 then keycode=`6`
    if PORTN.1 = 0 then keycode=`5`
if PORTN.0 = 0 then keycode=`4`
    PORTM = 0x3E                 ' K=1 J=1 H=1 G=0
    if PORTN.3 = 0 then keycode=`F`
    if PORTN.2 = 0 then keycode=`3`
    if PORTN.1 = 0 then keycode=`2`
if PORTN.0 = 0 then keycode=`1`


    if keycode==old_keycode then return 0
    old_keycode = keycode
    return keycode
    end function

 

Télécommande de l'araignée