bascom avr tft lcd quotation

The Bascom AVR SE version has been specially prepared for Polish customers. In exchange for a reduced price of the software, the manufacturer introduced two restrictions compared to the full version:

Attention! Bascom AVR SE is not intended for commercial use! The program can be upgraded to the full version of Bascom AVR - the cost of this update is the difference between the price of Bascom AVR and Bascom AVR SE.

Programming in Bascom language is relatively easy, because it normally contains a rich library of functions for handling peripheral modules built into the microcontroller (including AC converter, easy configuration and I / O port line control, support for serial transmission, EEPROM memory support).

However, the biggest advantage of Bascom AVR is the library of functions providing support for the most frequently used chips in microprocessor systems, eg character and graphical LCD displays, systems with SPI, I2C, UART and 1-Wire interfaces (software and hardware), PC keyboard and matrix keyboard. Among the advanced features, it is worth paying attention to the TCP / IP stack using the W3100A system.

Bascom AVR is a integrated development environment (IDE) designed for programming microcontrollers from the AVR family in a programming language of the same name. This language is similar to the Basic language.

After purchasing Bascom AVR please register on the manufacturer"s website: http://register.mcselec.com/login.php . This entitles you to a free upgrade.

bascom avr tft lcd quotation

Module with TFT LCD display 1.8" with a resolution of 128x160 px. It has a 8-pin connector with power pins and control lines. The display is controlled by the built-in ST7735 controller, which communicates via the SPI interface.

bascom avr tft lcd quotation

What is a computer? ................................................................................................................... 95 What does a computer system do? ............................................................................................. 95 What exactly is a microcontroller? ............................................................................................... 96 What does a microcontroller system do?..................................................................................... 97 What you do when learning to program ....................................................................................... 98 AVR microcontroller hardware .................................................................................................... 99 Power supplies............................................................................................................................ 99 BASCOM and AVR assignment ................................................................................................ 100 Programming words you need to be able to use correctly ......................................................... 102

11 Getting started with AVR Programming ........................................................................ 103 11.1 11.2 11.3 11.4 11.5 11.6 11.7 11.8

Breadboard ............................................................................................................................... 103 Breadboard+Prototyping board circuit ....................................................................................... 104 Alternative ATTiny461 breadboard circuit.................................................................................. 106 Alternative ATMega48 breadboard circuit ................................................................................. 107 Alternative ATMega breadboard circuit ..................................................................................... 108 AVR circuit description .............................................................................................................. 109 Output Circuit - LED .................................................................................................................. 110 AVR programming cable ........................................................................................................... 110

12 Getting started with Bascom & AVR .............................................................................. 111 12.1 12.2 12.3 12.4 12.5 12.6 12.7

The compiler ............................................................................................................................. 111 The programmer ....................................................................................................................... 111 An introduction to flowcharts ..................................................................................................... 112 Bascom output commands ........................................................................................................ 113 Introducing ‘bugs’ to see what happens .................................................................................... 114 Getting started code for the ATMega48 .................................................................................... 115 Getting started code for the ATMega8535................................................................................. 116

Variables - numbers inside the AVR.......................................................................................... 134 Pedestrian crossing lights controller .......................................................................................... 135 Pedestrian Crossing Lights schematic ...................................................................................... 136 Pedestrian Crossing Lights PCB Layout.................................................................................... 137 Algorithm planning example – pedestrian crossing lights .......................................................... 138 Flowchart planning example – pedestrian crossing lights .......................................................... 139 Program example - pedestrian crossing lights ........................................................................... 140 Modification exercise for the pedestrian crossing ...................................................................... 141 Changing a variable – simple stepping/counting ....................................................................... 142 For-next tricks with flashing LEDs ......................................................................................... 144 For-Next ................................................................................................................................ 145 Using variables for data......................................................................................................... 147 Types of memory .................................................................................................................. 147 Binary and Hexadecimal numbers ......................................................................................... 148 Learning to count in binary .................................................................................................... 149 Learning some Hexadecimal (HEX) ...................................................................................... 150 Rules about variables ............................................................................................................ 153 a few examples of variables in use ........................................................................................ 153 Random Numbers ................................................................................................................. 154 The Bascom-AVR simulator .................................................................................................. 155 Variables research assignment ............................................................................................. 157 Byte variable limitations......................................................................................................... 158 Electronic dice project ........................................................................................................... 159 Programming using variables – dice...................................................................................... 159 Dice layout stage 1................................................................................................................ 160 Dice layout stage 2................................................................................................................ 161 Dice Layout final.................................................................................................................... 162 First Dice Program flowchart ................................................................................................. 163 A note about the Bascom Rnd command .............................................................................. 164 Modified dice ......................................................................................................................... 165 Multiple LEDs - 7 segment displays....................................................................................... 167 Programming using variables - sound ................................................................................... 173 Make a simple siren .............................................................................................................. 175 Quiz Game Controller............................................................................................................ 177 Quiz game controller system context diagram ....................................................................... 177 Quiz game controller block diagram ...................................................................................... 178 Quiz game controller Algorithm ............................................................................................. 179 Quiz game schematic ............................................................................................................ 180 Quiz game board veroboard layout ....................................................................................... 181 Quiz Controller flowchart ....................................................................................................... 186 "Quiz Controller program code ............................................................................................... 187

31 LCDs (liquid crystal displays) ......................................................................................... 264 31.1 Alphanumeric LCDs .................................................................................................................. 265 ATTINY26 Development PCB with LCD ............................................................................................... 266 31.2 Completing the wiring for the LCD............................................................................................. 268 31.3 LCD Contrast Control ................................................................................................................ 269 31.4 Learning to use the LCD ........................................................................................................... 270 31.5 Adding more interfaces to the ATTiny26 Development board.................................................... 271 31.6 Ohms law in action – a multicoloured LED ................................................................................ 273 31.7 Repetition again - the ‘For-Next’ and the LCD ........................................................................... 275 31.8 Defining your own LCD characters ............................................................................................ 276 31.9 LCD custom character program ................................................................................................ 276 31.10 To CLS or not to CLS that is the question ............................................................................. 278 31.11 A simple digital clock ............................................................................................................. 279

Strings assignment ................................................................................................................... 296 ASCII Assignment ..................................................................................................................... 298 Time in a string ......................................................................................................................... 301 Scrolling message assignment .................................................................................................. 303 Some LCD programming exercises........................................................................................... 304

Microcontroller power limitations ............................................................................................... 305 Power ....................................................................................................................................... 307 Power dissipation in resistors .................................................................................................... 307 Diode characteristics ................................................................................................................. 308 Using Zener diodes ................................................................................................................... 309 How diodes work....................................................................................................................... 310 How does a LED give off light? ................................................................................................. 311 LCD Backlight Data ................................................................................................................... 312 Transistors as power switches .................................................................................................. 313 High power loads .................................................................................................................. 314 AVR Power matters............................................................................................................... 314 Darlington transistors - high power ........................................................................................ 316 ULN2803 Octal Darlington Driver .......................................................................................... 318 Connecting a FET backlight control to your microcontroller ................................................... 320 FET backlight control ............................................................................................................ 321

ADC - Analog to Digital conversion ........................................................................................... 344 Light level sensing .................................................................................................................... 344 Voltage dividers review ............................................................................................................. 345 AVR ADC connections .............................................................................................................. 345 Select-Case .............................................................................................................................. 346 Reading an LDR’s values .......................................................................................................... 348 Marcus’ Nightlight project .......................................................................................................... 350 Temperature measurement using the LM35 .............................................................................. 353 A simple temperature display .................................................................................................... 354 LM35 temperature display ..................................................................................................... 357 Voltage measurement using a voltage divider ....................................................................... 360 Variable power supply voltmeter program ............................................................................. 362 Force Sensitive Resistors ...................................................................................................... 364 Piezo sensor ......................................................................................................................... 364 Multiple switches and ADC .................................................................................................... 365

H-Bridge.................................................................................................................................... 373 H-Bridge Braking....................................................................................................................... 375 L293D H-Bridge IC .................................................................................................................... 376 L298 H-Bridge IC .................................................................................................................... 378 LMD18200 H-Bridge IC ............................................................................................................. 379 LMD18200 program .................................................................................................................. 382 Darlington H-Bridge .................................................................................................................. 383 Stepper motors ......................................................................................................................... 386 PWM - pulse width modulation .................................................................................................. 393 PWM outputs ........................................................................................................................ 394 Uses for PWM ....................................................................................................................... 395 ATMEL AVRs PWM pins ....................................................................................................... 396 PWM on any port .................................................................................................................. 397 PWM internals ....................................................................................................................... 398

40 AVR pull-up resistors ...................................................................................................... 400 41 Keypad interfacing........................................................................................................... 401 41.1 41.2 41.3 41.4 41.5 41.6

Analogue seconds display on an LCD ....................................................................................... 426 LCD big digits ........................................................................................................................... 429

Simplex and duplex ................................................................................................................... 499 Synchronous and asynchronous ............................................................................................... 499 Serial communications, Bascom and the AVR .......................................................................... 500 RS232 serial communications ................................................................................................... 501 Build your own RS232 buffer..................................................................................................... 503 Talking to an AVR from Windows XP ........................................................................................ 504 Talking to an AVR from Win7 .................................................................................................... 506 First Bascom RS-232 program .................................................................................................. 508 Receiving text from a PC .......................................................................................................... 509 BASCOM serial commands ................................................................................................... 510 Serial IO using Inkey() ........................................................................................................... 511 Creating your own software to communicate with the AVR ................................................... 514 Microsoft Visual Basic 2008 Express Edition ......................................................................... 515 Stage 1 – GUI creation .......................................................................................................... 516 Stage 2 – Coding and understanding event programming ..................................................... 525 Microsoft Visual C# commport application ............................................................................. 530 Microcontroller with serial IO. ................................................................................................ 535 PC software (C#) to communicate with the AVR ................................................................... 540 Using excel to capture serial data ......................................................................................... 544 PLX-DAQ .............................................................................................................................. 546 StampPlot ............................................................................................................................. 547 Serial to parallel .................................................................................................................... 549 Keyboard interfacing – synchronous serial data ........................................................................ 554 Keyboard as asynchronous data ............................................................................................... 561

56 Amplifier ........................................................................................................................... 608 57 Bike audio amplifier project ............................................................................................ 608 58 Graphics LCDs ................................................................................................................. 614 58.1 58.2 58.3

The T6963 controller ................................................................................................................. 614 Graphics LCD (128x64) – KS0108 ............................................................................................ 619 Generating a negative supply for a graphics LCD ..................................................................... 624

59 GLCD Temperature Tracking Project ............................................................................. 626 59.1 59.2 59.3 59.4 59.5 59.6 59.7

Low level languages:................................................................................................................. 638 AVR Internals – how the microcontroller works ......................................................................... 639 1. The 8bit data bus .................................................................................................................. 640 2. Memory ................................................................................................................................. 640 3. Special Function registers ..................................................................................................... 641 A simple program to demonstrate the AVR in operation ............................................................ 641

Keypad- polling versus interrupt driven ..................................................................................... 645 Improving the HT12 radio system by using interrupts ................................................................ 650 Magnetic Card Reader .............................................................................................................. 652 Card reader data structure ........................................................................................................ 652 Card reader data timing ............................................................................................................ 653 Card reader data formats .......................................................................................................... 654 Understanding interrupts in Bascom- trialling ............................................................................ 654 Planning the program ................................................................................................................ 657

69 SSD1928 based colour graphics LCD ............................................................................ 759 69.1 69.2 69.3 69.4 69.5 69.6 69.7 69.8 69.9 69.10 69.11 69.12 69.13 69.14 69.15 69.16 69.17 69.18 69.19 69.20 69.21 69.22

System block diagram ............................................................................................................... 759 TFT LCDs ................................................................................................................................. 760 System memory requirements .................................................................................................. 761 System speed ........................................................................................................................... 761 SSD and HX ICs ....................................................................................................................... 761 Colour capability ....................................................................................................................... 761 SSD1928 and HX8238 control requirements ............................................................................. 762 SSD1928 Software ................................................................................................................... 763 SSD1928 microcontroller hardware interface ............................................................................ 767 Accessing SSD control registers ........................................................................................... 768 SSD1928_Register_routines.bas .......................................................................................... 771 Accessing the HX8238. ......................................................................................................... 775 SSD1928_GPIO_routines.bas............................................................................................... 775 LCD timing signals ................................................................................................................ 777 HX setups ............................................................................................................................. 778 SSD setups ........................................................................................................................... 779 SSD line / HSync timing ........................................................................................................ 780 SSD row / VSync/ frame timing ............................................................................................. 781 HX and SSD setup routine .................................................................................................... 783 "SSD1928_HardwareSetup_Routines.bas ............................................................................. 783 SSD1928_Window_Control_Routines.bas ............................................................................ 787 Colour data in the SSD memory ............................................................................................ 790

Traffic Light help and solution ....................................................................................... 800 USB programmer - USBASP ........................................................................................... 806 USBTinyISP programmer ................................................................................................ 808 Various AVR development board schematics and layouts .......................................... 812 AVR Development Board 2 ....................................................................................................... 815 Dev board version 2 circuit diagram .......................................................................................... 816 Dev board pcb layout version 2 ................................................................................................. 817 ATMEGA V4b development board circuit – 12TCE 2011 .......................................................... 818 V4b devboard layout 12TCE 2011 ............................................................................................ 819 ATMega Dev PCB V5DSchematic (2012) ................................................................................. 820 ATMega Dev PCB V5DLayout (2012) ....................................................................................... 821 ATMega Dev PCB V5D Copper (2012) ..................................................................................... 822

128x64 GLCD Schematic – VerC -data on portB ...................................................................... 827 128x64 GLCD Layout – VerC –data on portB ........................................................................... 828 128x64 GLCD Schematic – VerD -data on portB ...................................................................... 829 128x64 GLCD Layout –VerD -data on portB ............................................................................. 830

1. Develop an understanding of what a computer is and build a correct mental model for one a. Input and output conversion at the voltage level b. Conversion of input and output voltages into data c. Processing and manipulating data which is stored in variables 2. Get to know about the hardware you are using a. Get a copy of the datasheet b. Learn about the power supply required c. Learn how to configure pins as either input or output d. Learn how to interface common I/O circuits: LED’s, Switches, Piezo, LDR… e. Find out about the different types of memory and amount of each f. Find out about the speed of processing 3. Get to know the language and the IDE you are using a. Learn to access the helpfile (e.g. highlight a word and press F1) b. The language has syntax (specific grammar/word rules) you must use correctly c. The IDE (Integrated Development Environment) has special commands and built in functions you must know and use: F7, F4, $crystal, $regfile, config, alias, const, port, pin d. Learn common I/O functions: set, reset, locate, LCD, GetADC e. Understand the limitations of and use variables: byte, word, long, single, double f. Use constants instead of numbers in the code (e.g. waitms timedelay) g. Get to know the control functions: Do-Loop (Until), For-Next, While-Wend, If-Then (Else) h. Get to know about text and math functions (read help file, write a few simple programs using the simulator) 4. Develop Algorithms (written plans for the process the program must carry out) a. Have a goal in mind for the program – use specifications and write a simple brief b. Plan your I/O by drawing a system block diagram c. Determine variables and constants required in the program d. Determine the state of all the I/O when the program begins e. Write the algorithm – Identify, order and describe the major processes the micro must do. 5. Draw Flowcharts or Statecharts (visual diagram for the process the program must carry out) a. Identify the blocks/states that will be used b. Use arrows to link the blocks and visualise control processes and program flow 6. Develop code from the flowcharts a. The outer looping line is replaced with a do-loop b. Backwards loops are replaced with do-loop do-loop-until, for-next, while-wend c. Forward loops are generally replaced with If-Then-EndIf d. Replace the blocks with actual commands e. Layout the code with correct indentations(tabs) to improve readability f. Learn to comment code so that it explains what is happening (not just describes) g. Use subroutines to organise complex code so that logic code is separate from I/O code h. Trial different ways of solving the problem and keep records of you experiments This is not a step by step process; as when you get to know about one area you get to know about others at the same time. The key to gaining depth in your knowledge and understanding comes from LOTS OF EXPERIMENTATION! That means making mistakes and above all having fun, you need to know that good decisions come from experience and experience comes from bad decisions!!! So experimenting is ok. In your electronics courses at school the aim is not to make you an expert in all the above (expertise comes after about 10 years working in an area), the aim is to introduce you to microcontroller electronics and programming, and to understand some of what is happening in the world around you and to feel able to see that you can control it and not have it control you. 98

A microcontroller is a general purpose electronic circuit; it is a full computer inside a single integrated circuit (IC or chip). Often ICs have fixed functions e.g. the TDA2822M amplifier or LM358 opamp, they only do one job and their input and output pins have fixed roles, so you have limited control over what they do, and therefore limited control over how to connect them. With a microcontroller however you are in control, you decide:  what the function of the IC is  what most of the pins are used for (inputs or outputs)  and what external input/output devices these pins are connected to. If you want an egg timer, a car alarm, an infrared remote control or whatever, it can all be done with a microcontroller. A commercial range of microcontrollers called ‘AVR’ is available from ATMEL (www.atmel.com). You could start with the ATTiny461, it has 4kbytes of Flash for program storage, 128 bytes of Ram and 128 bytes of EEPROM for long term data storage. Or you could start with the ATMega48, it has 4kbytes of Flash, 512 bytes of RAM and 256 bytes of EEPROM. ATTiny461

The AVR is a microcontroller from which manufacturer________________ The URL for their website is: ________________________ Download the specific datasheet for our microcontroller (the summary version not the full version) and print the first 2 pages and put them in your journal. The Programmable Memory size is ______ The SRAM size is _______The EEPROM size is _______ The number of I/O lines is __________ and they are arranged in _______ports

BASCOM-AVR is a compiler from _____________________ The URL for their website is: ________________________ Download the latest version of the BASCOM AVR demo and install it on your PC. There are a number of application notes on the website for the AVR Describe what AN128 is about __________________________________________________________________________ __________________________________________________________________________ __________________________________________________________________________

There are a number of other great resource websites for the AVR and BASCOM Find 3 websites on the internet that have useful resource information on BASCOM List the websites URL and what you found there __________________________________________________________________________ __________________________________________________________________________ __________________________________________________________________________ __________________________________________________________________________ __________________________________________________________________________ __________________________________________________________________________ __________________________________________________________________________

11 Getting started with AVR Programming Microcontrollers, such as the ATMEL AVR, are controlled by software and they can do nothing until they have a program inside them. The AVR programs are written on a PC using BASCOM-AVR. This software is a type of computer program called a compiler, it comes from www.mcselec.com. It comes in a freeware version so students may download it and use it at home. The AVR is connected to the PC with a 5 wire cable.

The 5 pin header (connector) is for programming the AVR from a PC. The 0.1uF capacitor between 0V and VCC is to reduce any variations in power supply voltage. The 10k is a pull-up resistor for the reset pin, a low (connection to ground) on this pin will halt the microcontroller and when it is released(pulled high by the resistor) the program will run from the beginning again. The 1N4148 is a protection diode that will stop high voltages possibly damaging the microcontroller (it is only required on the reset pin because all the other microcontroller pins have built in protection diodes). The 0.1uF capacitor and 100R resistor are the power supply for the ADC circuit

A five wire cable is needed to connect the AVR circuit to a PC. It connects the PC’s parallel port to the AVR circuit. One end has a DB25M connector on it (as in this picture)

12 Getting started with Bascom & AVR BASCOM-AVR is four programs in one package, it is known as an IDE (integrated development environment); it includes the Program Editor, the Compiler, the Programmer and the Simulator all together.

When you have successfully compiled a program pressing F4 or the green IC picture in the toolbar starts the programmer. If no microcontroller is connected an error will pop up. If the IC s connected then the BASCOM completes the programming process and automatically resets your microcontroller to start execution of your program.

Flash1LEDv1.bas Type the code below into BASCOM, save it, then F7 to compile and then F4 to program ‘ Flash1LEDv1.bas "-----------------------------------------------" Compiler Setup (this tell Bascom things about our micro) $regfile = "attiny461.dat" "bascom must know the micro $crystal = 1000000 "bascom must know its speed "-----------------------------------------------" Hardware Setups " (these tell bascom how to setup our micro) Config Porta = Output "LEDs on port "-----------------------------------------------" Declare Constants " (these tell bascom names we will use for numbers " in our program, this makes it easy "to change things quickly later) Const Flashdelay = 150 ‘ preset how long a wait will be "-----------------------------------------------Do ‘start of a loop Porta = &B10000000 ‘ LED 7 on Waitms Flashdelay ‘wait a preset time Porta = &B00000000 ‘all LEDs off Waitms Flashdelay ‘wait a preset time Loop ‘return to do and start again End YOU NEED TO INDENT CODE BETWEEN ALL CONTROL STRUCTURES SUCH AS WITH THIS DO-LOOP, it really helps make your code more readable and easier to debug! Use the TAB key in Bascom to do it.

This is a typical first program to test your hardware Every line of the code is important. $regfile=”attiny461.dat”, Bascom needs to know which micro is being used as each micro has different features; this is the name of a file in the Bascom program folder with every detail about the ATTiny461. $crystal=1000000, This line tells Bascom the speed at which our microcontroller is executing operations 1 million per second)so that Bascom can calculate delays such as waitms properly Config porta=output, each I/O must be configured to be either an input or output; (it cannot be both at once) Const Flashdelay=150, ‘constants’ are used in a program, it is easier to remember names and it is useful to keep them all together in one place in the program (this is a code of practice). DO - LOOP statements enclose code which is to repeat forever; when programming it is important to indent (tab) code within loops; this makes your code easier to follow (this is a code of practice). Waitms flashdelay wait a bit, a microcontroller carries out operations sequentially, so if there is no pause between turning an LED on and turning it off the led will not be seen flashing Output Code Porta = &B10000000 make porta.7 high (which will turn on the LED connected to that port) and make all the other 7 output pins on that port low Porta = 0 make all 8 pins on porta low (which will turn off any LEDs connected to that port)

In the code throughout this book different AVR microcontrollers are referred to in different places. So take special note of the $regfile and $crystal commands used and make sure they match the micro you are using. Note that the ATTiny461 and ATMega48 both run by default at 1,000,000MHz while the ATMega8535 runs at 8,000,000 MHz. You will also have to make changes to the ports used in the program: the ATTiny461 has ports A & B, the ATMega48 has B, C & D, the Amega8535 ahd ports A, B, C & D.

Note the change to $regfile and $crystal (If the timing of the flashing is wrong then try $crystal=1000000 (as AVRs often come setup at 1MHz not 8 MHz)

"this program shows how to write code which controls the whole port at "once using the commands portA=&B0000000, rather than individual set and ‘reset commands which are very wasteful of code space when multiple LEDs ‘have to "be contolled (excellence comment) "-----------------------------------------------------------------" Compiler Setup (these tell Bascom things about our micro) $regfile = "attiny461.dat" "bascom needs to know the micro $crystal = 1000000 "bascom needs to know its speed "-----------------------------------------------------------------" Hardware Setups (these tell bascom how to setup our micro) " setup direction of all ports Config Porta = Output "LEDs on portA Config Portb = Input "switches on portB " Hardware Aliases (these tell bascom names we will use for I/O devices " attached to the Micro, names are easier to remember that ports) Config Porta = Output Config Portb = Output "-----------------------------------------------------------------" Declare Constants (these tell bascom names we will use for numbers in " " our program, this makes it easy to change things quickly later) " times have been made shorter for testing purposes Const Delaytime = 25 Do Porta = &B10000000 "1 =A.7 Waitms Delaytime Porta = &B01000000 "2 =A.6 119

Bascom’s sound command can be used to directly make a tone. Piezo alias portA.3 Sound piezo, 500, 300 ‘that’s all that’s required The Bascom sound command has three parameters (values) attached ot it.  The port or pin of the microcontroller used  The duration of the sound (number of pulses)  The time the pin is high and low for. This command is not easy to use to get accurate tones from your AVR, but they do make useful sounds. Experiment with the sound command and make a series of tones suitable for an alarm. "-----------------------------------------------------------------" Compiler Setup (these tell Bascom things about our micro) $regfile = "attiny461.dat" "bascom needs to know the micro $crystal = 1000000 "bascom needs to know its speed "-----------------------------------------------------------------" Hardware Setups (these tell bascom how to setup our micro) " setup direction of all ports Config Porta = Output "LEDs on portA Config Portb = Output "switches on portB " Hardware Aliases (these tell bascom names we will use for I/O devices " attached to the Micro, names are easier to remember that ports) Leda4 Alias Porta.4 Piezo Alias Portb.3

"-----------------------------------------------------------------" Declare Constants (these tell bascom names we will use for numbers in " " our program, this makes it easy to change things quickly later) " times have been made shorter for testing purposes Const Flashdelay = 150

In this first program we would like the LED to change from off to on every time the switch is pressed. “When the switch is pressed change the LED” "-----------------------------------------" Compiler Setup (these tell Bascom things about our micro) $regfile = "attiny461.dat" "bascom needs to know the micro $crystal = 1000000 "bascom needs to know its speed "----------------------------------------" Hardware Setups (these tell bascom how to setup our micro) " setup direction of all ports Config Porta = Output "LEDs on portA Config Portb.5 = Input "switches on portB " Hardware Aliases (these tell bascom names we will use for I/O devices " attached to the Micro, names are easier to remember that ports) RedSw Alias Pinb.6 " hardware alias "---------------------------------------" Program starts here Do If Redsw = 0 Then "only do if switch pressed Toggle LED End If Loop End

We alter the program to wait a bit after the switch is pressed, which will give us time to release ithe switch “When the switch is pressed change the LED” "-----------------------------------------" Compiler Setup (these tell Bascom things about our micro) $regfile = "attiny461.dat" "bascom needs to know the micro $crystal = 1000000 "bascom needs to know its speed "----------------------------------------" Hardware Setups (these tell bascom how to setup our micro) " setup direction of all ports Config Porta = Output "LEDs on portA Config Portb.5 = Input "switches on portB " Hardware Aliases (these tell bascom names we will use for I/O devices " attached to the Micro, names are easier to remember that ports) RedSw Alias Pinb.6 " hardware alias "---------------------------------------" Program starts here Do If Redsw = 0Then " wait until switch press Toggle LED Waitms waitdelay End If Loop End

“If the switch is pressed, debounce the switch then toggle LED” "---------------------------------------" Compiler Setup (these tell Bascom things about our micro) $regfile = "attiny461.dat" "bascom needs to know the micro $crystal = 1000000 "bascom needs to know its speed "----------------------------------------" Hardware Setups (these tell bascom how to setup our micro) " setup direction of all ports Config Porta = Output "LEDs on portA Config Portb.5 = Input "switches on portB " Hardware Aliases (these tell bascom names we will use for I/O devices " attached to the Micro, names are easier to remember that ports) RedSw Alias Pinb.6 " hardware alias "---------------------------------------" Program starts here Do If Sw = 0 Then Waitms debouncetime Do Loop until Sw = 1 Waitms debouncetime Toggle Led End If Loop End

"-----------------------------------------------------------------" Title Block " Author: B.Collis " Date: 1 Aug 2008 " File Name: PedestrianCrossingLightsVer3.bas "-----------------------------------------------------------------" Program Description: " reads switch to check if pedestrian wants to cross " and changes the lights "-----------------------------------------------------------------" Compiler Setup (these tell Bascom things about our micro) $regfile = "attiny461.dat" "bascom needs to know the micro $crystal = 1000000 "bascom needs to know its speed "-----------------------------------------------------------------" Hardware Setups (these tell bascom how to setup our micro) " setup direction of all ports Config Porta = Output "LEDs on portA Config Portb = Input "switches on portB " Hardware Aliases (these tell bascom names we will use for I/O devices " attached to the Micro, names are easier to remember that ports) Greenlight Alias Porta.7 Orangelight Alias Porta.6 Redlight Alias Porta.5 Crossbutton Alias Pinb.6 Crossnowlight Alias Porta.3 Dontcrosslight Alias Porta.4 "-----------------------------------------------------------------" DIMENSION VARIABLES (a variable can be changed during the program, so we might have different delays at different times of the day (maybe before and after school we could change the orange and cross delay)

"----------------------------------------------" Compiler Setup $regfile = "attiny461.dat" $crystal = 1000000 "bascom needs to know its speed "-----------------------------------------------" Hardware Setups " setup direction of all ports Config Porta = Output "LEDs on portA Config Portb = Input "switches on portB " Hardware Aliases Green Alias Porta.1 Red Alias Porta.0 "-----------------------------------------------" Declare Constants Const Orangedelay = 7 Const Crossdelay = 20 Const Dontcrossdelay = 5 "----------------------------------------------" Program starts here "Declare Constants Const Lightontime = 500 Const Lightofftime = 2500 "---------------------------------------------"Declare Variables Dim Count As Byte "----------------------------------------------Program starts here Do For Count = 1 to 20 Set Red Waitms Lightontime Reset Red Waitms Lightofftime Incr Count Next For Count = 1 to 20 Set Green Waitms Lightontime Reset Green Waitms Lightofftime Incr Count Next Loop End

1.Type in the program COMPLETE THE MISSING PARTS, save it, Compile (F7) and Simulate it (F2). In the simulator window Step A: Choose the menu button with LCD, to open Hardware Simulation Step B: Select the switch inputs PinB.0 through PinB.4 as high (this simulates pullup resistors) Step C: Select the upper most blank yellow box in the variable area. Then double click it to get a drop down box. Then choose cars Do this for the 3 other variables

Variabes must start with a letter not a digit e.g. Dim Red_cars As Byte not Dim 1cars As Byte Variabes must not be Bascom reserved(special) words e.g. Dim band As Byte not Dim And As Byte Variables must contain no spaces e.g. Dim Red_cars As Byte not Dim Red cars As Byte Variable names should relate to what the variable is used for e.g. Dim Red_cars As Byte, not Dim hgashg As Byte Variable names cannot be used for other things such as constants or subroutines e.g. Dim Red_cars As Byte, means yu cannot have Const Red_cars = 12 as well

The simulator is an ideal tool for testing small parts of a program to see if you achieved what you wanted to. We will use it to explore the numerical range of different types of variables Here is some code to show off a few BASIC commands. Copy this program into BASCOM and compile it and see if you can understand what is happening and why. " ShowComandsV1.bas . $sim $crystal = 1000000 $regfile = "attiny26.dat" Config Porta = Output Config Portb = Output Config Pinb.6 = Input "dimension variables Dim Byte1 As Byte Dim Byte2 As Byte Dim Word1 As Word Dim Int1 As Integer Dim Single1 As Single Dim Single2 As Single

Every microcontroller has different amount of RAM available for storing variables Carry out research on these different AVR microcontrollers RAM size (bytes)

First lets review what a tone is. It is a repeated turning on and off of our piezo. The frequency of the tone is 1/period. The duration of the tone is the number of complete cycles. A piezo will not make a sound when you turn it on; it only makes a sound when you turn it on and off rapidly. So to make a tone we must turn the piezo on then wait a bit, then turn it off and we repeat this for the duration of the tone. In this program the tone period will be 1mS so the piezo must be on for 500uS (1/2 mS) and off for the same. Bascom has a waitus command (it is not particularly accurate but its good enough for this exercise). We want the tone to last long enough to hear it so we need to repeat it 150 times. 150 times 1mS will give us a tone duration of 150mS (0.15S). To count the number of cycles we will dimension a variable called cyclecount, and we will increase it inside a do-loop. It will count upto the max number of cycles and then we will have a 2 second break. Then it will repeat. Remember to reset cycle count to 0 or it will overflow. This program works similalrly to the Bascom SOUND command.

"-----------------------------------------------------------------" Title Block " Author: B.Collis " Date: 22 Feb 08 " File Name: SirenV1.bas "-----------------------------------------------------------------" Program Description: " This program makes a simple tone using a piezo " Program Features: " makes use of Bascom waitus (microseconds) command " introduces first use of a variable " the variable cyclecount increases from 0 until it reaches the preset (constant) " value maxcyclecount at which point there is a quiet time " the led is on when the the tone is occuring " Hardware Features " a pezo can be directly connected to the micro port " the led has a 1k resistor in series to limit the current "-----------------------------------------------------------------" Compiler Directives (these tell Bascom things about our hardware) $regfile = "attiny26.dat" "the micro we are using $crystal = 1000000 "rate of executing code "-----------------------------------------------------------------" Hardware Setups Config Portb = Output " Hardware Aliases Piezo Alias Portb.3 "use useful name PIEZO not PORTb.3 Blueled Alias Portb.4 "use useful name BLUELED not PORTB.4 "-----------------------------------------------------------------"Declare Constants Const Halfperioddelay = 500 " delay for 1/2 period Const Maxcyclecount = 150 "number of cycles to do "-----------------------------------------------------------------" Declare Variables Dim Cyclecount As Byte "-----------------------------------------------------------------" Program starts here Do Set Blueled "turn led on For Cyclecount = 0 to Maxcyclecount1 Waitus Halfperioddelay1 Set Piezo Waitus Halfperioddelay1 Reset Piezo Next Reset Blueled "turn led off Waitms 2000 "quiet time Loop End "end program

"-----------------------------------------------------------------" Title Block " Author: B.Collis " Date: 22 Feb 08 " File Name: SirenV2.bas "-----------------------------------------------------------------" Program Description: " This program makes a simple siren on a piezo " " Program Features: " makes use of bascom waitus (microseconds) command " Hardware Features: "-----------------------------------------------------------------" Compiler Directives (these tell Bascom things about our hardware) $crystal = 1000000 "the crystal we are using $regfile = "attiny26.dat" "the micro we are using "-----------------------------------------------------------------" Hardware Setups " setup direction of all ports Config Portb = Output " Hardware Aliases Piezo Alias Portb.3 "use useful name PIEZO not PORTb.3 Blueled Alias Portb.4 "use useful name BLUELED not PORTB.4 "-----------------------------------------------------------------"Declare Constants Const Halfperioddelay1 = 500 " first tone 1/2 period Const Halfperioddelay2 = 800 " second tone 1/2 period Const Maxcyclecount1 = 350 " longer than 255!!! Const Maxcyclecount2 = 150 "-----------------------------------------------------------------" Declare Variables Dim Cyclecount As Word "keep count of number of cycls(periods) Dim Sirens As Byte 175

it keeps the files that BASCOM generates for your program in one place this helps you find programs quickly when you want to it is less confusing it is good practice Save your program at the beginning when you start it, this helps guard against teachers that like to turn the computers off unexpectedly.

"-----------------------------------------------------------------" Compiler Directives (these tell Bascom things about our hardware) $regfile = "attiny26.dat" "the micro we are using $crystal = 1000000 "the speed of the micro "-----------------------------------------------------------------" Hardware Setups " setup direction of all ports Config Porta = Output "LEDs on portA Config Portb =Input "switches on portB " Hardware Aliases Led0 alias portb.0 " Initialise ports so hardware starts correctly Porta = &B11111111 "turns off LEDs "-----------------------------------------------------------------" Declare Variables " Initialise Variables " Declare Constants "-----------------------------------------------------------------" Program starts here Do Loop End

The use of subroutines as well as comments, aliases and constants will make your code easier to understand and maintain . Uncommentedandpoorlysetoutcodeislikereadingasentencewithoutpunctuationorspacestheme aningisstilltherebutitisalittlehardtofollowandunderstand. "-----------------------------------------------------------------" Title Block " Author: B. Collis " Date: May 2008 " File Name: MorseMeV2.bas "-----------------------------------------------------------------" Program Description: " send morse code using an LED " show off good use of subroutines and repitition "-----------------------------------------------------------------" Compiler Directives (these tell Bascom things about our hardware) $regfile = "attiny26.dat " bascom needs to know our micro $crystal = 1000000 " bascom needs to know how fast it is going "-----------------------------------------------------------------" Hardware Setups Config Porta = Output " make these micro pins outputs " Hardware Aliases Morseled Alias Porta.7 " the name morseled is easy to remember "-----------------------------------------------------------------" Declare Variables Dim Count As Byte "temporary variable to count repitions " Initialise Variables " Declare Constants Const Dotdelay = 250 " length of a dot Const Dashdelay = 750 " length of a dash Const Partofletterdelay = 250 " gap between parts of letters Const Endofletterdelay = 750 " gap between letters Const Endofworddelay = 1750 " gap between words "-----------------------------------------------------------------" Program starts here Do "start of a loop Gosub Send_c Gosub Send_l Gosub Send_s Waitms Endofworddelay "send more letters here Wait 5 Loop "return to do and start again End

Learning to develop useful planning tools to help solve problems such as drawings, block diagrams, tables & flowcharts. Learning about the Bascom commands SET, RESET and ALIAS 1. Understand the situation by drawing a planning diagram that explains the road layout

Then add things that it might have, an LCD, buttons, piezo, LEDs. Use the rectangle and circular buttons and other shapes to add to the device. Make sure that links between the micro and inputs/outputs are made in the right direction either coming in to the micro or out of it. Blocks are used to represent parts of the circuit, so an LED subsystem is created by just adding a circle and calling it red led. You do not show the current limit resistor, detail for that will be in the schematic. Sometimes it may be a good idea to have two separate block diagrams, one for I/O (input and output) devices and a second for the power supply (it just makes it easier to separate the two parts of your design). On the right hand side of the diagram are tables that list the outputs, inputs and variables that are created. These will be modified in later diagrams. The detail about port connections is useful in developing the setup program code for your program. By clicking on the Basic Code button in the toolbar the program code to form the setup area in your program will be automatically generated.

A new flowchart file starts with a template that is the minimum needed for a microcontroller program to function. The first block sets up the Bascom Compiler to recognise your specific micro e.g. $crystal = $regfile = As well as your specific hardware e.g. Config Port... And then variables your program will use to store data e.g. Dim car_count as byte

31 LCDs (liquid crystal displays) There are a great many different types of LCD available, we describe them by there various attributes. Colour/Monochrome, alphanumeric/graphic. Some LCDs which are made for specific purposes with fixed Characters such as these two.

One of the best things about electronic equipment nowadays are the alphanumeric LCD displays these are simple single, double or 4 line displays for text and numbers. These displays are becoming cheaper and cheaper in cost, we buy them in bulk from China using www.alibaba.com. The LCD is a great output device and with Bascom so very easy to use. They fit the need for student learning in technology education very nicely. Some common commands are  cls - clear the screen  LCD "Hello" - will display hello on the display  locate y,x - line and position on the line of the cursor (where text will appear)  Cursor OFF – hide the cursor (still there but invisible)  LCD temperature – will display the value in the variable temperature on the display Connecting an LCD to the microcontroller is not difficult. There are 14 or 16 pins on the LCD 1. 0V 2. +5V 3. Contrast 4. RS - register select 5. R/W - read/not write 6. E - Enable 7. D0 8. D1 9. D2 10. D3 11. D4 12. D5 13. D6 14. D7 15. Backlight + (optional) 16. Backlight 0V (optional) Most LCDs are set up so that they can communicate in parallel with either 4 bits or 8 bits at a time. The faster system is 8 bits as all the data or commands sent to the LCD happen at the same time, with 4 bit operation the data/command is split into 2 parts and each is sent separately. Hence it takes twice as long. Apart from the 4 data lines another couple of lines are necessary, these are control lines, RS , R/W, E. When using Bascom the R/W line is connected permanently to griund, and the other two lines need to be connected to the micro. The advantage of 4 bit operation is that the LCD uses only 6 I/O lines in total on the micro. At the current time the contrast line can be connected to ground as well.

ATTINY26 Development PCB with LCD Although a breadboard was useful earlier for some introductory learning about connecting a microcontroller and interfacing simple components such as LEDs and switche;, trying to use a breadboard to connect an LCD is not easy, you just end up with too many wires that fall out of the breadboard if the LCD gets moved. It is more useful to have a circuit board of some description. Here is a development PCB that was designed to be useful for students when building their circuits. It makes use of a standard 2 line 20 character alphanumeric LCD. It has a 16 way connector (although the LCD used has no backlight so only 14 connections are used)

In the schematic we have connected the power to the LCD but not actually connected the control lines. These are left unconnected so that students become familiar with the connections, it also made the PCB much easier for students to solder not having so many thin tracks.

Take care when wiring the header pins (connector) for the LCD as he polarity for the power must be correct, there is a an area for prototyping other circuits on the board PCB tracks view from Eagle

Here are the details for the specific Sure Electronics LCD we are using. Highlighted are the 6 data and control connections we need to make (note that pins 1,2,3,5 are already connected via PCB tracks). The two control lines are RS(register select) and Enable. The 4 data lines are DB4 to DB7.

Looking at the development board it can be seen that there are already pads for the LCD, The 6 connections have been added on the diagram below. The order the 6 lines are connected from the LCD to the micro does not matter as long as 1. They are on the same port and 2. the order used matches the configuration command in Bascom. To program the LCD using Bascom we need to add two lines of configuration program code, and then use specific commands to make the display show something

In addition to the 4 data lines and the 3 control lines, there are two more pins on the LCD for power (5V-VDD and 0V-VSS) and one for adjusting the contrast or viewing angle (VO or VEE). Check the displays’ datasheet to find out what is required for VO however for almost all modern alphanumeric type LCDs the voltage is often very close to 0V so can be connected to 0V directly. You can connect via a potentiometer or trimpot so that it is adjustable as in this circuit. HThe voltage divider here is made up of both fixed and a variable resistance. If the trim pot was 10k and the resistor was 47 k then the voltage for the contast would be

The first thing to learn about is how to