AVR MICRO CONTROLLER BASIC ROBOT (atmega8)
This video is my first AVR atmega8 project in my workshop check this out if any one wants a help on it let me reply ill answer
051 Tutorial: Timers
- 11,059,000 / 12 = 921,583
- 50,000 / 921,583 = .0542
- .05 * 921,583 = 46,079.15.
SFR Name
|
Description
|
SFR Address
|
TH0
|
Timer 0 High Byte
|
8Ch
|
TL0
|
Timer 0 Low Byte
|
8Ah
|
TH1
|
Timer 1 High Byte
|
8Dh
|
TL1
|
Timer 1 Low Byte
|
8Bh
|
TCON
|
Timer Control
|
88h
|
TMOD
|
Timer Mode
|
89h
|
When you enter the name of an SFR into an assembler, it internally converts it to a number. For example, the command:
- MOV TH0,#25h
- MOV 8Ch,#25h
- TH0 * 256 + TL0 = 1000 3 * 256 + 232 = 1000
- TMOD (89h) SFR
Bit
|
Name
|
Explanation of Function
|
Timer
|
7
|
GATE1
|
When this bit is set the timer will only run when INT1 (P3.3) is high. When this bit is clear the timer will run regardless of the state of INT1.
|
1
|
6
|
C/T1
|
When this bit is set the timer will count events on T1 (P3.5). When this bit is clear the timer will be incremented every machine cycle.
|
1
|
5
|
T1M1
|
Timer mode bit (see below)
|
1
|
4
|
T1M0
|
Timer mode bit (see below)
|
1
|
3
|
GATE0
|
When this bit is set the timer will only run when INT0 (P3.2) is high. When this bit is clear the timer will run regardless of the state of INT0.
|
0
|
2
|
C/T0
|
When this bit is set the timer will count events on T0 (P3.4). When this bit is clear the timer will be incremented every machine cycle.
|
0
|
1
|
T0M1
|
Timer mode bit (see below)
|
0
|
0
|
T0M0
|
Timer mode bit (see below)
|
0
|
TxM1
|
TxM0
|
Timer Mode
|
Description of Mode
|
0
|
0
|
0
|
13-bit Timer.
|
0
|
1
|
1
|
16-bit Timer
|
1
|
0
|
2
|
8-bit auto-reload
|
1
|
1
|
3
|
Split timer mode
|
Machine Cycle
|
TH0 Value
|
TL0 Value
|
1
|
FDh
|
FEh
|
2
|
FDh
|
FFh
|
3
|
FDh
|
FDh
|
4
|
FDh
|
FEh
|
5
|
FDh
|
FFh
|
6
|
FDh
|
FDh
|
7
|
FDh
|
FEh
|
- TCON (88h) SFR
Bit
|
Name
|
Bit Address
|
Explanation of Function
|
Timer
|
7
|
TF1
|
8Fh
|
Timer 1 Overflow. This bit is set by the microcontroller when Timer 1 overflows.
|
1
|
6
|
TR1
|
8Eh
|
Timer 1 Run. When this bit is set Timer 1 is turned on. When this bit is clear Timer 1 is off.
|
1
|
5
|
TF0
|
8Dh
|
Timer 0 Overflow. This bit is set by the microcontroller when Timer 0 overflows.
|
0
|
4
|
TR0
|
8Ch
|
Timer 0 Run. When this bit is set Timer 0 is turned on. When this bit is clear Timer 0 is off.
|
0
|
- MOV TCON, #80h
- SETB TF1
- MOV TMOD,#01h
- SETB TR0
REPEAT:
|
MOV A,TH0
|
|
MOV R0,TL0
|
|
CJNE A,TH0,REPEAT
|
|
…
|
- MOV TH0,#76;High byte of 19,457 (76 * 256 = 19,456) MOV TL0,#01;Low byte of 19,457 (19,456 + 1 = 19,457) MOV TMOD,#01;Put Timer 0 in 16-bit mode SETB TR0;Make Timer 0 start counting JNB TF0,$;If TF0 is not set, jump back to this same instruction
JNB P1.0,$
|
;If a car hasn’t raised the signal, keep waiting
|
JB P1.0,$
|
;The line is high which means the car is on the sensor right now
|
INC COUNTER
|
;The car has passed completely, so we count it
|
Chapter 8 8051 Microcontroller – Serial Port Operations / Communication
Chapter 88051 Microcontroller –
|
|||||||||||||||||||||||||||
|
8051 Tutorial: Serial Communication
Bit
|
Name
|
Bit Addres
|
Explanation of Function
|
7
|
SM0
|
9Fh
|
Serial port mode bit 0
|
6
|
SM1
|
9Eh
|
Serial port mode bit 1.
|
5
|
SM2
|
9Dh
|
Mutliprocessor Communications Enable (explained later)
|
4
|
REN
|
9Ch
|
Receiver Enable. This bit must be set in order to receive characters.
|
3
|
TB8
|
9Bh
|
Transmit bit 8. The 9th bit to transmit in mode 2 and 3.
|
2
|
RB8
|
9Ah
|
Receive bit 8. The 9th bit received in mode 2 and 3.
|
1
|
TI
|
99h
|
Transmit Flag. Set when a byte has been completely transmitted.
|
0
|
RI
|
98h
|
Receive Flag. Set when a byte has been completely received.
|
SM0
|
SM1
|
Serial Mode
|
Explanation
|
Baud Rate
|
0
|
0
|
0
|
8-bit Shift Register
|
Oscillator / 12
|
0
|
1
|
1
|
8-bit UART
|
Set by Timer 1 (*)
|
1
|
0
|
2
|
9-bit UART
|
Oscillator / 64 (*)
|
1
|
1
|
3
|
9-bit UART
|
Set by Timer 1 (*)
|
- TH1 = 256 – ((Crystal / 384) / Baud)
- TH1 = 256 – ((Crystal / 192) / Baud)
- TH1 = 256 – ((Crystal / 384) / Baud) TH1 = 256 – ((11059000 / 384) / 19200 ) TH1 = 256 – ((28,799) / 19200) TH1 = 256 – 1.5 = 254.5
- TH1 = 256 – ((Crystal / 192) / Baud) TH1 = 256 – ((11059000 / 192) / 19200) TH1 = 256 – ((57699) / 19200) TH1 = 256 – 3 = 253
- 1. Configure Serial Port mode 1 or 3. 2. Configure Timer 1 to timer mode 2 (8-bit auto-reload). 3. Set TH1 to 253 to reflect the correct frequency for 19,200 baud. 4. Set PCON.7 (SMOD) to double the baud rate.
- MOV SBUF,#A
- CLR TI ;Be sure the bit is initially clear MOV SBUF,#A ;Send the letter A to the serial port JNB TI,$ ;Pause until the TI bit is set.
- JNB RI,$ ;Wait for the 8051 to set the RI flag MOV A,SBUF ;Read the character from the serial port
8051 Microcontroller Interrupts
8051 Tutorial: Interrupts
As the name implies, an interrupt is some event which interrupts normal program execution.
- JNB TF0,SKIP_TOGGLE CPL P3.0 CLR TF0 SKIP_TOGGLE: …
- CPL P3.0 RETI
- Timer 0 Overflow.
- Timer 1 Overflow.
- Reception/Transmission of Serial Character.
- External Event 0.
- External Event 1.
Interrupt
|
Flag
|
Interrupt Handler Address
|
External 0
|
IE0
|
0003h
|
Timer 0
|
TF0
|
000Bh
|
External 1
|
IE1
|
0013h
|
Timer 1
|
TF1
|
001Bh
|
Serial
|
RI/TI
|
0023h
|
Bit
|
Name
|
Bit Address
|
Explanation of Function
|
7
|
EA
|
AFh
|
Global Interrupt Enable/Disable
|
6
|
–
|
AEh
|
Undefined
|
5
|
–
|
ADh
|
Undefined
|
4
|
ES
|
ACh
|
Enable Serial Interrupt
|
3
|
ET1
|
ABh
|
Enable Timer 1 Interrupt
|
2
|
EX1
|
AAh
|
Enable External 1 Interrupt
|
1
|
ET0
|
A9h
|
Enable Timer 0 Interrupt
|
0
|
EX0
|
A8h
|
Enable External 0 Interrupt
|
- MOV IE,#08h
- or
SETB ET1
- SETB ET1 SETB EA
- External 0 Interrupt
- Timer 0 Interrupt
- External 1 Interrupt
- Timer 1 Interrupt
- Serial Interrupt
Bit
|
Name
|
Bit Address
|
Explanation of Function
|
7
|
–
|
–
|
Undefined
|
6
|
–
|
–
|
Undefined
|
5
|
–
|
–
|
Undefined
|
4
|
PS
|
BCh
|
Serial Interrupt Priority
|
3
|
PT1
|
BBh
|
Timer 1 Interrupt Priority
|
2
|
PX1
|
BAh
|
External 1 Interrupt Priority
|
1
|
PT0
|
B9h
|
Timer 0 Interrupt Priority
|
0
|
PX0
|
B8h
|
External 0 Interrupt Priority
|
- Nothing can interrupt a high-priority interrupt–not even another high priority interrupt.
- A high-priority interrupt may interrupt a low-priority interrupt.
- A low-priority interrupt may only occur if no other interrupt is already executing.
- If two interrupts occur at the same time, the interrupt with higher priority will execute first. If both interrupts are of the same priority the interrupt which is serviced first by polling sequence will be executed first.
- The current Program Counter is saved on the stack, low-byte first.
- Interrupts of the same and lower priority are blocked.
- In the case of Timer and External interrupts, the corresponding interrupt flag is cleared.
- Program execution transfers to the corresponding interrupt handler vector address.
- The Interrupt Handler Routine executes.
- Two bytes are popped off the stack into the Program Counter to restore normal program execution.
- Interrupt status is restored to its pre-interrupt status.
INT_SERIAL:
|
JNB RI,CHECK_TI
|
;If the RI flag is not set, we jump to check TI
|
|
MOV A,SBUF
|
;If we got to this line, its because the RI bit *was* set
|
|
CLR RI
|
;Clear the RI bit after weve processed it
|
CHECK_TI:
|
JNB TI,EXIT_INT
|
;If the TI flag is not set, we jump to the exit point
|
|
CLR TI
|
;Clear the TI bit before we send another character
|
|
MOV SBUF,#A
|
;Send another character to the serial port
|
EXIT_INT:
|
RETI
|
- CLR C ;Clear carry MOV A,#25h ;Load the accumulator with 25h ADDC A,#10h ;Add 10h, with carry
- PUSH ACC PUSH PSW MOV A,#0FFh ADD A,#02h POP PSW POP ACC
- PSW
- DPTR (DPH/DPL)
- PSW
- ACC
- B
- Registers R0-R7
- PUSH R0
- PUSH 00h
- Register Protection: Make sure you are protecting all your registers, as explained above. If you forget to protect a register that your main program is using, very strange results may occur. In our example above we saw how failure to protect registers caused the main program to apparently calculate that 25h + 10h = 51h. If you witness problems with registers changing values unexpectedly or operations producing “incorrect” values, it is very likely that you’ve forgotten to protect registers. ALWAYS PROTECT YOUR REGISTERS.
- Forgetting to restore protected values: Another common error is to push registers onto the stack to protect them, and then forget to pop them off the stack before exiting the interrupt. For example, you may push ACC, B, and PSW onto the stack in order to protect them and subsequently pop only ACC and PSW off the stack before exiting. In this case, since you forgot to restore the value of “B”, an extra value remains on the stack. When you execute the RETI instruction the 8051 will use that value as the return address instead of the correct value. In this case, your program will almost certainly crash. ALWAYS MAKE SURE YOU POP THE SAME NUMBER OF VALUES OFF THE STACK AS YOU PUSHED ONTO IT.
- Using RET instead of RETI: Remember that interrupts are always terminated with the RETI instruction. It is easy to inadvertantly use the RET instruction instead. However, the RET instruction will not end your interrupt. Usually, using a RET instead of a RETI will cause the illusion of your main program running normally, but your interrupt will only be executed once. If it appears that your interrupt mysteriously stops executing, verify that you are exiting with RETI.
8052 Tutorial
- 256 bytes of Internal RAM (compared to 128 in the standard 8051).
- A third 16-bit timer, capable of a number of new operation modes and 16-bit reloads.
- Additional SFRs to support the functionality offered by the third timer.
That’s really about all there is to the difference between the 8051 and 8052. The remainder of this tutorial will explain these additional features offered by the 8052, and how they are used within user programs. Throughout this tutorial, it is assumed that you already have read the 8051 Tutorial and have a thorough understanding of it.256 BYTES OF INTERNAL RAM
MOV R0,#90h ;Set the indirect address to 90h MOV A,@R0 ;Read the contents of Internal RAM pointed to by R0 The above code first assigns the value 90h to the register R0. It subsequently reads, indirectly, the contents of the address contained in R0 (90h). Thus, after these two instructions have executed, the Accumulator will contain the value of Internal RAM address 90h.It is very important to understand that the above code is not the same as the following:
MOV A,90h ;Reads the contents of SFR 90h (P1) This instruction uses direct addressing; recall that direct addressing reads Internal RAM when the address is in the range of 00h through 7Fh, and reads an SFR when the address is in the range of 80h through FFh. Thus in the case of this second example, the move instruction reads the value of SFR 90h-which happens to be P1 (I/O Port 1).NEW SFRs FOR 8052’S THIRD TIMER
BIT
|
NAME
|
BIT
ADDRESS |
DESCRIPTION
|
7
|
TF2
|
CFh
|
Timer 2 Overflow. This bit is set when T2 overflows. When T2 interrupt is enabled, this bit will cause the interrupt to be triggered. This bit will not be set if either TCLK or RCLK bits are set.
|
6
|
EXF2
|
CEh
|
Timer 2 External Flag. Set by a reload or capture caused by a 1-0 transition on T2EX (P1.1), but only when EXEN2 is set. When T2 interrupt is enabled, this bit will cause the interrupt to be triggered.
|
5
|
RCLK
|
CDh
|
Timer 2 Receive Clock. When this bit is set, Timer 2 will be used to determine the serial port receive baud rate. When clear, Timer 1 will be used.
|
4
|
TCLK
|
CCh
|
Timer 2 Receive Clock. When this bit is set, Timer 2 will be used to determine the serial port transmit baud rate. When clear, Timer 1 will be used.
|
3
|
EXEN2
|
CBh
|
Timer 2 External Enable. When set, a 1-0 transition on T2EX (P1.1) will cause a capture or reload to occur.
|
2
|
TR2
|
CAh
|
Timer 2 Run. When set, timer 2 will be turned on. Otherwise, it is turned off.
|
1
|
C/T2
|
C9h
|
Timer 2 Counter/Interval Timer. If clear, Timer 2 is an interval counter. If set, Timer 2 is incremented by 1-0 transition on T2 (P1.0).
|
0
|
CP/RL2
|
C8h
|
Timer 2 Capture/Reload. If clear, auto reload occurs on timer 2 overflow, or T2EX 1-0 transition if EXEN2 is set. If set, a capture will occur on a 1-0 transition of T2EX if EXEN2 is set.
|
TIMER 2 AS A BAUD-RATE GENERATOR
- NOTE: When Timer 2 is used as a baud rate generator (either TCLK or RCLK are set), the Timer 2 Overflow Flag (TF2) will not be set.
Chapter 5 8051 – Program Flow
Chapter 58051 – Program Flow |
||||||||||||||||||||||||||||||||
|
8051 Tutorial: Program Flow
LJMP NEW_ADDRESS
|
|
.
|
|
.
|
|
.
|
|
NEW_ADDRESS:
|
….
|
- The SJMP command, like the conditional branching instructions, can only jump to an address within +/- 128 bytes of the SJMP command.
- The AJMP command can only jump to an address that is in the same 2k block of memory as the AJMP command. That is to say, if the AJMP command is at code memory location 650h, it can only do a jump to addresses 0000h through 07FFh (0 through 2047, decimal).
Chapter 6 8051 – Low Level Information
Chapter 68051 – Low Level Information |
||||
|
8051 Tutorial: Instruction Set, Timing, and Low-Level Info
In order to understand–and better make use of–the 8051, it is necessary to understand some underlying information concerning timing.
- 11,059,000 / 12 = 921,583
Chapter 3 8051 – Basic Registers
Chapter 38051 – Basic Registers |
|||||||||||||||||||||||||||||||||||
|
8051 Tutorial: Basic Registers
- ADD A,R4
- MOV A,R3 ;Move the value of R3 into the accumulator ADD A,R4 ;Add the value of R4 MOV R5,A ;Store the resulting value temporarily in R5 MOV A,R1 ;Move the value of R1 into the accumulator ADD A,R2 ;Add the value of R2 SUBB A,R5 ;Subtract the value of R5 (which now contains R3 + R4)
Chapter 4 8051 – Addressing Modes
Chapter 48051 – Addressing Modes |
||||||||||||||||||||||||||||||
|
8051 Tutorial: Addressing Modes
An “addressing mode” refers to how you are addressing a given memory location. In summary, the addressing modes are as follows, with an example of each:
Immediate Addressing
|
MOV A,#20h
|
Direct Addressing
|
MOV A,30h
|
Indirect Addressing
|
MOV A,@R0
|
External Direct
|
MOVX A,@DPTR
|
Code Indirect
|
MOVC A,@A+DPTR
|
- MOV A,#20h
- MOV A,30h
- MOV A,@R0
- MOV R0,#99h ;Load the address of the serial port MOV @R0,#01h ;Send 01 to the serial port — WRONG!!
- MOVX A,@DPTR MOVX @DPTR,A
- MOVX @R0,A
Chapter 2 8051 – Special Function Registers
Chapter 2
8051 – Special Function Registers
Chapter 2
8051 Tutorial: SFRs
Chapter 1 8051 – Types of Memory
Chapter 18051 – Types of Memory |
|||||||||||||||||||||||||||||||||
|
8051 Tutorial: Types of Memory
- ADD A,R4
- ADD A,04h
- SETB 24h
- MOV 50h,#01h
- MOV 99h,#01h
8051 Tutorial: Introduction
- A general knowledge of programming.
- An understanding of decimal, hexidecimal, and binary number systems.
- A general knowledge of hardware.