' ********************************************************************** ' ** Program: gmbade.BAS - Version : 1.1 - 20 June 2005 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Board : GMB HR168 and GMM AM128 ** ' ** Society: grifo(r) ITALIAN TECHNOLOGY ** ' ** Via Dell' Artigiano 8/6 40016 San Giorgio di Piano (BO) ** ' ** Tel.+39 051 892 052 Fax +39 051 893 661 ** ' ** http://www.grifo.com http://www.grifo.it ** ' ** Written by: Graziano Gaiba ** ' ********************************************************************** ' ' 20.06.05 - Rel 1.1 By Graziano Gaiba ' This demo shows continuously on the console the combination read from ' analog input on pin 8 of CN7 on GMB HR 168, corresponding to ADC0 of micro. ' External Vref generated by GMB HR 168 is used. ' ' ' Compiled file is smaller than 2048 bytes, so it can becompliled with free ' demo of BASCOM AVR. ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options | Compiler | Chip, set: ' ' Chip: M128 ' HW Stack: at least 64 ' Soft Stack: at least 32 ' Framesize: at least 64 ' ' $regfile = "m128def.dat" $crystal = 7372800 $baud = 19200 ' ' ********************* Constans declarations ************************* ' Const Cret = 13 ' Carriage return Const Nl = 10 ' New line Const Clrscr = 12 ' Clear screen Const Bell = 7 ' Bell ' Time out in case a TWI command receives no response Const Ee_timeout = 50000 ' ' ********************* Variables declarations ************************ ' Dim I As Byte , Conv_h As Byte , Conv_l As Byte ' '*********** Configuration of internal devices and interrupt ************ ' ' Configured AD converter for single conversion, use internal Vref and ' automatic prescaler. ' Config Adc = Single , Prescaler = Auto , Reference = Internal ' ' '****************** Procedures declartion ********************** ' Declare Sub Init() ' Initializtion Declare Sub Check_ready() Declare Function Check_ready_2() As Byte ' '*************************** Main program ****************************** ' Main: Portf.4 = 1 ' Turn OFF relays Portf.5 = 1 Portf.6 = 1 Portf.7 = 1 Portb.3 = 1 Portb.2 = 1 Portb.1 = 1 Porte.2 = 1 Print Chr(clrscr); ' Clears the screen Print "Demo 1.1 for GMM AM128 + GMB HR168" Print Call Check_ready() Do Print "A/D converter demo" Print Print "Analog signal present on pin 8 of CN7 is converted." Print "Connecting J11 in 1-2 the signal can range from 0 to 2.5 V," Print "connecting J11 in 2-3 the signal can range from 0 to 10 V." Print "External 2.5 V reference voltage generated by GMB HR168 is used." Print "POLLING" Print Print "CH00" Start Adc ' Enables A/D Converter Do Admux = 0 ' Selects channel ADC0 ' and external Vref Adcsr.adsc = 1 ' Begin conversion Do Loop Until Adcsr.adif = 1 ' Waits for conversion end Adcsr.adif = 1 ' Resets end-of-conversion flag Conv_l = Adcl ' Always read least significant byte Conv_h = Adch Print Hex(conv_h) ; Hex(conv_l) ; ' Prints conversion Print Chr(cret); ' Carriage return Waitms 100 ' Delay for transmission I = Inkey() Loop Until I <> 0 Stop Adc Loop End ' ' *************************** Program end ****************************** ' ' '****************************************************************************** '* Demo specific procedures * '****************************************************************************** ' ' ' Initializes demo ' Sub Init() ' Initializes directionality of ports that drive optocouped inputs and relay ' outputs: ' IN1-1 - Portb.7 ' IN2-1 - Portb.6 ' IN3-1 - Porte.4 ' IN4-1 - Porte.5 ' IN5-1 - Portd.7 ' IN6-1 - Porte.6 ' IN7-1 - Portb.4 ' IN8-1 - Portb.0 Ddrb.7 = 0 Ddrb.6 = 0 Ddre.4 = 0 Ddre.5 = 0 Ddrd.7 = 0 Ddre.6 = 0 Ddrb.4 = 0 Ddrb.0 = 0 ' IN1-2 - Portc.0 ' IN2-2 - Portc.1 ' IN3-2 - Portc.2 ' IN4-2 - Portc.3 ' IN5-2 - Portc.4 ' IN6-2 - Portc.5 ' IN7-2 - Portc.6 ' IN8-2 - Portc.7 Ddrc = 0 ' OUT A1 - Portf.4 ' OUT A2 - Portf.5 ' OUT B1 - Portf.6 ' OUT B2 - Portf.7 ' OUT C1 - Portb.3 ' OUT C2 - Portb.2 ' OUT D1 - Portb.1 ' OUT D2 - Porte.2 (by default, that is J10 in 3-4) Ddrf.4 = 1 Ddrf.5 = 1 Ddrf.6 = 1 Ddrf.7 = 1 Ddrb.3 = 1 Ddrb.2 = 1 Ddrb.1 = 1 Ddre.2 = 1 End Sub ' '****************************************************************************** '* Generic procedures * '****************************************************************************** ' ' ' ' Supporting function for Check_ready(). ' Function Check_ready_2() As Byte Local Time_out As Word , I_n As Byte Time_out = 0 ' Timeout counter Do Incr Time_out ' Increment counter Loop Until Twcr.twint = 1 Or Time_out = Ee_timeout I_n = Twsr I_n = I_n And &HF8 Check_ready_2 = I_n End Function ' ' ' Check for card ready ' Sub Check_ready() Local Check As Byte , Test As Byte Do ' Loop to check for card ready Twsr = Twsr And &HFC Check = Twdr Or &HF4 Test.4 = Not Check.3 Twbr = 72 Twcr = &HA4 Check = Check_ready_2() Test = Check Or Twcr If Check = &H09 Then Test = &H67 Else Test.1 = Not Test.1 End If Twdr = &HA0 Twcr = &H84 Check = Check_ready_2() Twcr = &H94 Waitms 27 Loop Until Check = &H18 Or Test <> 1 Twcr.twen = 0 End Sub