' ********************************************************************** ' ** Program: gmbdae.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 uses Timer 1 of Mini Module to generate, on CN7, a PWM signal ' with preset frequency and duty cycle programmable in percent by console. ' Such signal, connected to an opportune intergrating circuit (RC network, ' operational amplifire with capacitor on feedback, etc.) allows to obtain an ' anlog signal like the one of a D/A. Demo execution depends on which Mini ' Module is used and serial line configuration. ' ' 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 declaration ********************** ' ' Generic use Dim S As Word , Char As Byte ' Generic use and SetPwm Dim S1 As String * 1 ' '****************** Procedures declartion ********************** ' Declare Sub Init() ' Initialization Declare Sub Set_relays_out(byval V As Byte) ' Set relays status Declare Function Leggi_char() As Byte ' Read one character (blocking) Declare Sub Check_ready() Declare Function Check_ready_2() As Byte ' '************************** Main program ****************************** ' Main: Do Call Init() ' Initialize module 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() Print Print "Timer 1 is configured as 10 bit PWM (output on pin 6 of CN7 on GMB HR168)." Do Inputhex "Duty cycle in hex (E.g. 300): " , S Ocr1ah = High(s) ' Set duty cycle Ocr1al = Low(s) Print "E to exit or any other key to re-set duty cycle" Char = Leggi_char() Loop Until Char = "E" Tccr1b = 0 ' Stop Timer 1 Loop End ' '************************ Program end *************************** ' ' '****************************************************************************** '* Demo specific procedures * '****************************************************************************** ' ' ' Initialize 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 Ddrb.5 = 1 ' Set portd.5 as output Ocr1ah = &H02 Ocr1al = &H00 Tcnt1h = 0 ' Reset timer Tcnt1l = 0 ' Timer 1 configured for Fast PWM, No Prescaler, toggles 1 pin 6 of CN7 ' on every match between register OCR1A and counter register of Timer 1, does ' not use OCR1B. Tccr1a = &H83 ' Start Timer 1 Tccr1b = &H09 End Sub ' '****************************************************************************** '* Generic procedures * '****************************************************************************** ' ' ' ' Reads a character in blocking way,converts it in capital letter and prints it ' Function Leggi_char() As Byte Local C As Byte C = Waitkey() Print Chr(c) C = C And &HDF ' Converts ASCII character in capital letter Leggi_char = C End Function ' ' ' Supporting function for Check_ready(). ' Function Check_ready_2() As Byte Local Time_out As Word , I As Byte Time_out = 0 ' Timeout counter Do Incr Time_out ' Increment counter Loop Until Twcr.twint = 1 Or Time_out = Ee_timeout I = Twsr I = I And &HF8 Check_ready_2 = I 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