' programme pour gérer le capteur vitesse sur scooter
' 31 octobre 2005 . Avec good affichage pour le F1
' B. HOAREAU
' Version Cboy F1
' Mettre un condensateur 68nf sur base transistor
' pour filtrer parasite hf

#include "startcf1.bas"
       
   
        INT compteur,tour,compteur2
	DDRA.7 = 0   ' Entrée
        DDRA.5 = 1   ' Sortie	
	lcdinit()
      
        ' Initialisation pour mode interruption externe sur A7
        PACTL.7=0
        PACTL.5=0   ' Mode comptage impulsion
        PACTL.4=0
        PACTL.6=1   ' validation PAEN
        TMSK2.4=1   ' Autorisation int sur A7
        TFLG2.4=1   ' flag associé
        TMSK2.6=0   ' stop timer
        TFLG2.6=0          		   
    
	cli			' autoriser les inter
       	print " Scoot Elec "

        do
         compteur = 0
         PORTA.5 = 1                 ' Pour calibrer tempo sur oscillo
         tempo(89)
         PORTA.5 = 0
         compteur2=compteur
       ' print "compteur=",compteur2      'nombre d'impusion pendant la tempo
         tour = compteur2*64/32            ' 60/32
         print "#1 v=",tour," tr/min "
       loop

function tempo(cnt)
	int i, k
	for cnt=cnt to 0 step -1
		for i=0 to 1000
		next
	next
	end function


        interrupt function rtiint at $FFDA
          
	' Se déclenche à la fin de l'impulsion sur A7 (front descendant)
        compteur = compteur + 1   ' Comptage des impulsions capteurs

	TFLG2.4 = 1		' autoriser ints a nouveau
	end function

//********* driver LCD ****************************************

#define LCDLINES 2			//+++ 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
