;*************************************************************************** ; ; File Name :'MAIN.asm" ; Title : ; Date : ; Version : ; Support telephone :765 287 1987 David B. VanHorn ; Support fax :765 287 1989 ; Support Email :dvanhorn@cedar.net ; Support Snail ;1104 E 13th St, Muncie IN 47302 ; Target MCU :AT90S8515 ; ;***************************************************************************; ; D E S C R I P T I O N ; ; This is an application I put together to help people get started on the ;ATMEL AVR 8515 processor. It's not complete by any means, I dont' think it's ;possible to use EVERY feature and mode. Basically, I wanted to get you started, ;and help you avoid some sneaky traps that you can fall into. ; ;Legal issues: I retain ownership of this software. I grant you the right to use ;the software to educate yourself on the intricacies of the 8515 processor, and ;to use it for personal, non-commercial use. Anything else, email me. If you think ;this is a cool thing and want to encourage me to write more stuff, feel free to ;send a donation. ; ;This application uses most (all?) of the features of the assembler itself, in ;terms of equates, defines, inline math, multiple file includes.. ; ;This application uses timer 0 and timer 1, the UART and baud rate generator, ;all the I/O ports, and the watchdog timer. ; ;Active features: ; ; Pseudorandom number (PN) generator, This is implemented with a 19 bit ; maximal length shift register. Each time the routine is called, the ; bit pattern is shifted. The seed value is programmable, and the routine ; leaves you randomized bits in three registers (Could easily be put in SRAM) ; ; 1mS system "tick" (T0) Drives timeouts and timed events ; ; Watchdog timer support, coded for the internal watchdog, but a simple ; hack to drive external doggies. There is an equate to set the watchdog ; "feed" interval, driven from the 1mS opsys tick ; ; R/C Servo outputs. (T1) There is a programmable width for each servo, from ; 0-255 (actually 250, the math for 255 is JUST TOO UGLY!) Eight or less ; output channels are supported. Each channel has a byte in ram that sets ; it's position. There is a debug routine in the servo logic that provides ; some action on channels 1-3 so you can see it do something immediately. ; WARNING: Buffer the signal with at least a 1k resistor out to the servos. ; ; Switch inputs (ok, so it's not hard..) ; LED outputs, driven from the PN generator. Ditto ; ; Stepper motor control. A single channel output for unipolar motors, with ; flag bytes in RAM setting full or half step mode, and forward or reverse ; direction, as well as speed in mS per step (or half-step) ; WARNING: You're on your own for drivers, the 8515 won't run even the ; wimpiest stepper directly, don't even try. At a minimum, four NPN transistors ; should do the trick. ; ; Buffered serial input and output comms at popular baud rates. Also break ; generation, and Xon/Xoff handshake. Coding in hardware handshake would be ; trivial. There is a receive and a transmit buffer of configurable size. ; ; Inter-Charachter delay on transmit. Not pacing with nulls, this is a true ; delay, driven by the T0 interrupt. I've needed this from time to time to ; talk to other devices that couldn't really handle full speed comms. ; ; ;This application does not use the I2C interface, or the on-chip EEPROM. ;Feel free to do something with them and send me a module to include in the package. ; ;PLEASE DO NOT HACK THIS AND PASS IT ON. ;Hack it for yourself all you like, but the main idea is to give newcomers software ;that does something, and that actually WORKS.. Your code might work, but the next ;guy might not be so clever. If you use this to create something else, please call it ;something else, and make it clear to whoever you give it to that it's YOUR creation. ; ;Old software rule #1: You modify it, you're on your own. ; ;***************************************************************************; ; M O D I F I C A T I O N H I S T O R Y ; ; ; rev. date who why ; ---- -------- --- ------------------------------------------ ; 0.01 98.07.29 dvh Creation ; 0.02 98.08.20 dvh Adding RC servo outputs on PORTC ; 0.03 98.08.30 dvh Added watchdog timer support ; 0.04 98.08.30 dvh Added stepper motor support ; 0.05 98.08.31 dvh Exported Watchdog support to it's own file ; 0.10 98.09.02 dvh Couldn't think of anything else to add, so I shipped it. ; ; 0.11 98.10.15 dvh Added DS1820 and 1602 support on port A ; Moved "Ping" to port A0 ; Fixed a stack bug if the serout buffer was full ; At this point, just less than half the ROM is used. ; ; 0.12 98.11.30 dvh Added several more devices to the brew, but they are not ; actually part of this app. I ran out of I/O pins. If you ; want to use LCD4bit, VFD, or DS1806, you'll have to give ; something up from this app. The switches and LEDs take ; up a lot of pins, so they would be good candidates. ; The code is out there, with examples of how to set up the ; I/O pins required, just alter them to the pins you choose. ; ;******************************************************************** ;This application consists of fifteen files. They should all be in the ;same directory, or you will have to edit the .include directives ;below to make them point to the right locations. ; ;Main.asm (You're looking at it) The main structure of the program ;8515def.inc Port and pin definitions, supplied by ATMEL ;Equates.inc Where constants get defined as words, to make life easier ;Isr.asm Interrupt Service Routines ;Init.asm Machine initialization ;Memory.asm Buffer management ;Serial.asm Serial comms using the on-board uart ;Servo.asm Servo control code for eight standard R/C servos on PORTC ;Stepper.asm Stepper motor control for one unipolar motor on port A0-A3 ;Random.asm A 19 bit PseudoNoise generator ;Watchdog.asm Watchdog timer code ;DS1602.asm Handler for the Dallas 1602 dual seconds counter clock ;DS1820.asm Handler for the Dallas 1820 thermometer chip ;Tables.asm Example lookup table, plus code to get data out of it. ; ;The following files are not integrated into this application, they are just ;example code. ; ;DS1806.asm Handler for Dallas sextet pot ;LCD4bit.asm Handler for generic charachter based LCDs in 4 bit mode ;VFD.asm Handler for Futaba Vaccum Flourescent Displays ;EEPROM.asm Handler for the internal EEPROM, coded to save and recall ; pot values for DS1806 above, but you can use it for anything. ; ;******************************************************************** ;Physical resources. ; ;This application is designed for an AVR demo board, with 8.00 MHz crystal. ;For bonus points, modify it to use a 4 MHz xtal, without changing the speed ;that anything runs at. That's a good first project! ; ;A0-3 Stepper motor output, 4 phase unipolar motor (Ping on A0 if used) ;A4-6 DS1602 dual RTC chip A4=/Reset A5=Data A6=Clock ;A7 DS1820 Thermostat chip ;B0-7 Demo board LEDs ;D0-7 Demo board switches ;C0-7 R/C Servo outputs, connect to servos through a 1k resistor. ;T0 1mS opsys interrupt ;T1 Servo width ;UART Interrupt driven buffered comms, both directions ;Wdog Internal watchdog timer is active. ; ;******************************************************************** ; ;Logical equates to make life easier ; .include "EQUATES.INC" ;Lots of settings in here to play with. ; ;******************************************************************** ; ;The actual beginning of the code. ;INIT the machine, start the ISRs, and exit into idle ;On reset, the 8515 jumps to 0000, which contains a jump vector (see ISR.ASM) ; .cseg ;This tells the assembler that what follows is code, and goes in ROMspace ; .include "Isr.asm" ;Restart vector and Interrupt service routines are here .include "Init.asm" ;Initialization of timers,ports,and ext hdw. ; ;*********************************************************************** ; Idle: ldi TEMP,'A' ; sts MORSE,TEMP ; rcall ASC2MORSE ; ; ;Spin the random number generator. ; rcall Random ;Result in RAND1,RAND2,and RAND3 out PORTB,RAND1 ;Output to the lights ;Without a scope, you'll just see lights that are half-bright. ; ;Get the switches and light the lights (currently turned off) ;The switches are used in SERVO.ASM to control some of the servo outputs. ;in TEMP,PIND ;Input the switches ;out PORTB,TEMP ;Output to the lights ;NOTE! The switch inputs are active low, an un-pushed switch ;gives a "1" input. This results in LED off, if the pin status ;is fed out to PORTB, due to how the LEDs are wired. ;NOTE! A LOW output state lights the LEDs. NOT a HIGH!!! ;NOTE: The servos have width values set in INIT, and min-max values set in EQUATES. ;You should set the min-max to match your servos, and load the widths you want into ;the servo control bytes. rcall Servo_Frame_Check ;Move the servos, if it's time (Servo.asm) ;NOTE: The stepper is set to OFF in init, you'll need to set a direction, mode ;and step-rate before you'll get any output. ; rcall Step_Motor ;Step the stepper, if it's time (Stepper.asm) ;You could put something here to react to RS-232 input, and/or create RS-232 ;output based on switch inputs, the random generator, send messages.. Up to you! ;You could write a subsumption based robot controller, I've given you a bunch ;of bot-useful I/O functions that really work, and a ton of free CPU cycles. ;The step motor driver needs an interface capable of handling the load, but ;the output pins are wiggling properly, just don't try to connect a stepper ;directly to the chip. A set of four NPN transistors will work acceptably ;well for small motors with the motor common leads (2 usually) tied to a ;suitable power supply. Don't try to run steppers off the development board ;power supply, you'll glitch the CPU. ;The RTC and temp sensor code just needs the occasional call to update the ;RAM buffers. The RTC should be called as needed, or at least 2x per second ;if you expect the RTCBUF to be a random-access thing. ;ldi TEMP,$01 ;A ping every time we loop, this shouldn't be used ;rcall Ping ;unless your'e debugging, it will glitch the stepper. rcall Timed_Smack ;Feed the watchdog, if it's needed (Watchdog.asm) rjmp Idle ; ;************************************************************************************* ;Useful Hooks! Nothing in this section gets executed, it's just examples. ; ;You don't want to continuously call the clock routines, I just put them here so it ;would be obvious how to use them. ; ;The DS1602 has two seconds counters. One is battery backed (RTIME) and one isn't (ETIME) ;The GET routines dump their data in RTC_BUF, and the PUT routines take their input from RTC_BUF ;There are separate sections for RTIME data and ETIME data. ; ;rcall Get_RTIME ;Read the DS1602 ;rcall Put_RTIME ;Write to the DS1602 ;rcall Clear_RTIME ;Clear the battery-backed counter ;rcall Get_ETIME ;Read from the DS1602 ;rcall Put_ETIME ;Write to the DS1602 ;rcall Clear_ETIME ;Clear the elapsed time counter ;DS1820 Thermostat routines ;Uses a 9 byte buffer at TEMP_BUF, an 8 byte buffer at SERNO_BUF, ;and two byte flags "TEMP_FLAG",and "TEMP_STAT" ; ;Ram Data: ;Byte 0 Temp LSB First byte transmitted ; 1 Temp MSB ; 2 TH/User 1 ; 3 TH/User 2 ; 4 Reserved ; 5 Reserved ; 6 Count remain ; 7 Count per Deg C. ; 8 CRC Last byte transmitted ; ;rcall Update_Temp ;The main routine, calls Start, Poll, and Get. ; ;rcall Start_Temp ;Starts the conversion process, checks TEMP_FLAGto see if it's already ; ;running, so you can harmlessly call it over and over. ;rcall Poll_Temp ;Checks to see if it's done converting, sets TEMP_FLAG if so ;rcall Get_Temp ;Actually get the temp data, once POLL says we can, returns TEMP_FLAG to idle ; ;rcall Get_Serno ;Pick up the lasered serial number from the chip, called already in INIT, ; ;so you shouldn't need to mess with it later, just get the data from RAM. ; ;rcall Reset_1820 ;Initializes the chip, Returns 00 in TEMP_STAT if the sensor is ok, ; ;or FF if no sensor was detected. Call this only in INIT, or if something ; ;goes very wrong. ; ;Basically, you could call Update_Temp whenever you're not doing anything ;else, and it will drop an updated temperature into the buffer whenever it can. ; ;******************************************************************** ; ;A terribly useful little diagnostic hack, provided you've got a scope. ;Just call Ping with a number in TEMP, and it will give you N pulses. ;This is very useful for figuring which branch of a program is taken ;on live hardware, when simulation isn't really an option. ; Ping: sbi PORTA,0 ;I hope your scope can resolve to 125nS cbi PORTA,0 ; dec TEMP ;One less ping brne Ping ;Are we done? If not, then ping again ret ;If so, bye! ;******************************************************************** ;External Routines ; .include "Servo.asm" ;RC Servo drivers, 8 channels on port C using timer 1 .include "Stepper.asm" ;5/6 wire unipolar motor driver .include "Memory.asm" ;Memory allocation, buffer handlers .include "Serial.asm" ;Serial port I/O .include "Random.asm" ;Pseudorandom generator, Specifically, a 19 ;bit maximal length generator. .include "Watchdog.asm" ;Watchdog timer handler .include "DS1602.asm" ;Dallas DS1602 RTC chip handler .include "DS1820.asm" ;Dallas DS1820 Thermostat chip .include "morse.asm" ;ASCII text to Morse code converter ;*************************************************************************** ;Lookup tables.. Stuff you need ; .include "Tables.asm" ; .db "COPYRIGHT 1997-1998 David VanHorn",CR,LF .db "ALL RIGHTS RESERVED",CR,LF ; ;***************************************************************************