Previous Next
Action
Syntax
Remarks
This specifies which reference options are available. The possible values are
listed in the table below.
INTERNAL or INTERNAL_1.1
tiny15,tiny26 AVCC 2
OFF
INTERNAL
INTERNALEXTCAP
tiny13 AVCC 3
INTERNAL
tiny24,tiny44,tiny85 AVCC 4
EXTERNAL or OFF
INTERNAL or INTERNAL_1.1
m164,m324,m644,m640,m1280, AREF or OFF 5
m1281,m2561,m2560
AVCC
INTERNAL1.1
INTERNAL_2.56
tiny261,tiny461,tiny861, AVCC 7
tiny25,tiny45,tiny85
EXTERNAL or OFF
INTERNAL_1.1
INTERNAL_2.56_NOCAP
INTERNAL_2.56_EXTCAP
CAN128, PWM2_3,USB1287, m128, AREF or OFF 8
m16, m163, m32, m323, m64
AVCC
INTERNAL or INTERNAL_2.56
When you use VALUE=value, you may specify any value. The disadvantage is that when you port your code
from one chip to another it will not work.
While the AREF, AVCC, etc. are all converter to the right settings, the value can not be converted.
The AD converter is started automatic when you use the CONFIG ADC command.
You can use STOP ADC and START ADC to disable and enable the power of the AD converter.
See also
GETADC
Example
'--------------------------------------------------------------------------
------
'name : adc.bas
'micro : Mega163
' Getadc() will also work for other AVR chips that have an ADC converter
'--------------------------------------------------------------------------
------
$crystal = 4000000
'With STOP ADC, you can remove the power from the chip
'Stop Adc
Channel = 0
Do
W = Getadc(channel)
Incr Channel
Loop
End
'Using the additional param on chip that do not have the internal reference
will have no effect.
GETADC Top Previous Next
Action
Syntax
Remarks
Var The variable that is assigned with the A/D value. This should be a Word or
other 16 bit variable.
Channel The channel to measure. Might be higher then 7 on some chips. The Mega2560
has 16 channels. So the range is 0-15 on a Mega2560.
Offset An optional numeric variable of constant that specifies gain or mode. This
option has effect on newer AVR micro’s only. The offset will be added by the
channel value and inserted into the ADMUX register. This way you can control
gain.
The GETADC() function only will work on microprocessors that have an A/D converter.
The pins of the A/D converter input can be used for digital I/O too.
But it is important that no I/O switching is done while using the A/D converter.
Make sure you turn on the AD converter with the START ADC statement or by setting the proper bit in the
ADC configuration register.
Some micro’s have more then 7 channels. This is supported as well. The ADCSRB register contains a bit named
MUX5 that must be set when a channel higher then 7 is used. The compiler (lib routine) will handle this
automatic. This is true for new chips like Mega1280, Mega2560 and probably other new chips with 100 pins.
This will read channel 0 and 1. The offset is 64 in order to use singled ended input.
ADC8 is portK.0
GetADC() returns a word variable since the A/D converter data registers consist of 2 registers. The resolution
depends on the chip.
The variable ADCD can be used to access the data register directly. The compiler will handle access to the byte
registers automatically.
See also
CONFIG ADC
Example
'--------------------------------------------------------------------------
------
'name : adc.bas
'micro : Mega163
'--------------------------------------------------------------------------
------
$crystal = 4000000
Start Adc
'With STOP ADC, you can remove the power from the chip
'Stop Adc
Channel = 0
Do
W = Getadc(channel)
Incr Channel
Loop
End
'Using the additional param on chip that do not have the internal reference
will have no effect.
START Top Previous Next
Action
Syntax
START device
Remarks
You must start a timer/counter in order for an interrupt to occur (when the external gate is disabled).
The AC and ADC parameters will switch power to the device and thus enabling it to work.
See also
STOP
Example
'--------------------------------------------------------------------------
------
'name : adc.bas
'micro : Mega163
' Getadc() will also work for other AVR chips that have an ADC converter
'--------------------------------------------------------------------------
------
$crystal = 4000000
Start Adc
'With STOP ADC, you can remove the power from the chip
'Stop Adc
Channel = 0
Do
W = Getadc(channel)
Incr Channel
Loop
End
'Using the additional param on chip that do not have the internal reference
will have no effect.
Action
Syntax
$FRAMESIZE = var
Remarks
While you can configure the Frame Size in Options, Compiler, Chip, it is good practice to put the value into
your code. This way you do no need the cfg(configuration) file.
The $FRAMESIZE directive overrides the value from the IDE Options.
It is important that the $FRAMESIZE directive occurs in your main project file. It may not be included in an
$include file as only the main file is parsed for $FRAMESIZE
See also
$SWSTACK, $HWSTACK
Example
'--------------------------------------------------------------------------
------
'name : adc.bas
'micro : Mega163
' Getadc() will also work for other AVR chips that have an ADC converter
'--------------------------------------------------------------------------
------
$crystal = 4000000
Action
Syntax
$HWSTACK = var
Remarks
While you can configure the HW Stack in Options, Compiler, Chip, it is good practice to put the value into your
code. This way you do no need the cfg(configuration) file.
The $HWSTACK directive overrides the value from the IDE Options.
It is important that the $HWSTACK directive occurs in your main project file. It may not be included in an
$include file as only the main file is parsed for $HWSTACK.
The Hardware stack is room in RAM that is needed by your program. When you use GOSUB label, the
microprocessor pushes the return address on the hardware stack and will use 2 bytes for that. When you use
RETURN, the HW stack is popped back and the program can continue at the proper address. When you nest
GOSUB, CALL or functions, you will use more stack space. Most statements use HW stack because a machine
language routine is called.
See also
$SWSTACK , $FRAMESIZE
Example
'--------------------------------------------------------------------------
------
'name : adc.bas
'micro : Mega163
' Getadc() will also work for other AVR chips that have an ADC converter
'--------------------------------------------------------------------------
------
$crystal = 4000000
Action
Syntax
$SWSTACK = var
Remarks
While you can configure the SW Stack in Options, Compiler, Chip, it is good practice to put the value into your
code. This way you do no need the cfg(configuration) file.
The $SWSTACK directive overrides the value from the IDE Options.
It is important that the $SWSTACK directive occurs in your main project file. It may not be included in an
$include file as only the main file is parsed for $SWSTACK
See also
$HWSTACK , $FRAMESIZE
Example
'--------------------------------------------------------------------------
------
'name : adc.bas
'micro : Mega163
' Getadc() will also work for other AVR chips that have an ADC converter
'--------------------------------------------------------------------------
------
$crystal = 4000000
Action
Syntax
STOP device
STOP
Remarks
The single STOP statement will end your program by generating a never ending loop. When END is used it will
have the same effect but in addition it will disable all interrupts.
The STOP statement with one of the above parameters will stop the specified device.
The AC and ADC parameters will switch power off the device to disable it and thus save power.
See also
START , END
Example
A summary of the instruction set mnemonics and their parameters is given here. For a detailed description of
the Instruction set, refer to the AVR Data Book.
Adiw r24, K6
SBIW Rdl, K6 Subtract Immediate from Rdh:Rdl = Rdh:Rdl Z,C,N,V,S 2
Word -K
Sbiw R24,K6
MUL Rd,Rr Multiply Unsigned R1, R0 = Rd * Rr C 2*
BRANCH
INSTRUCTIONS
RJMP K Relative Jump PC = PC + k + 1 None 2
IJMP Indirect Jump to (Z) PC = Z None 2
JMP K Jump PC = k None 3
RCALL K Relative Call Subroutine PC = PC + k + 1 None 3
ICALL Indirect Call to (Z) PC = Z None 3
CALL K Call Subroutine PC = k None 4
RET Subroutine Return PC = STACK None 4
RETI Interrupt Return PC = STACK I 4
CPSE Rd,Rr Compare, Skip if Equal if (Rd = Rr) PC = None 1/2
PC + 2 or 3
CP Rd,Rr Compare Rd - Rr Z,C,N,V,H, 1
CPC Rd,Rr Compare with Carry Rd - Rr - C Z,C,N,V,H 1
CPI Rd,K Compare with Immediate Rd - K Z,C,N,V,H 1
SBRC Rr, b Skip if Bit in Register If (Rr(b)=0) PC = None 1/2
Cleared PC + 2 or 3
SBRS Rr, b Skip if Bit in Register Set If (Rr(b)=1) PC = None 1/2
PC + 2 or 3
SBIC P, b Skip if Bit in I/O Register If(I/O(P,b)=0) PC = None 2/3
Cleared PC + 2 or 3
SBIS P, b Skip if Bit in I/O Register If(I/O(P,b)=1) PC = None 2/3
Set PC + 2 or 3
BRBS s, k Branch if Status Flag Set if (SREG(s) = 1) None 1/2
then PC=PC+k + 1
BRBC s, k Branch if Status Flag if (SREG(s) = 0) None 1/2
Cleared then PC=PC+k + 1
BREQ K Branch if Equal if (Z = 1) then PC = None 1/2
PC + k + 1
BRNE K Branch if Not Equal if (Z = 0) then PC = None 1/2
PC + k + 1
BRCS K Branch if Carry Set if (C = 1) then PC = None 1/2
PC + k + 1
BRCC K Branch if Carry Cleared if (C = 0) then PC = None 1/2
PC + k + 1
BRSH K Branch if Same or Higher if (C = 0) then PC = None 1/2
PC + k + 1
BRLO K Branch if Lower if (C = 1) then PC = None 1/2
PC + k + 1
BRMI K Branch if Minus if (N = 1) then PC = None 1/2
PC + k + 1
BRPL K Branch if Plus if (N = 0) then PC = None 1/2
PC + k + 1
BRGE K Branch if Greater or if (N V= 0) then PC None 1/2
Equal, Signed = PC+ k + 1
BRLT K Branch if Less Than, if (N V= 1) then PC None 1/2
Signed = PC + k + 1
BRHS K Branch if Half Carry Flag if (H = 1) then PC = None 1/2
Set PC + k + 1
BRHC K Branch if Half Carry Flag if (H = 0) then PC = None 1/2
Cleared PC + k + 1
BRTS K Branch if T Flag Set if (T = 1) then PC = None 1/2
PC + k + 1
BRTC K Branch if T Flag Cleared if (T = 0) then PC = None 1/2
PC + k + 1
BRVS K Branch if Overflow Flag if (V = 1) then PC = None 1/2
is Set PC + k + 1
BRVC K Branch if Overflow Flag if (V = 0) then PC = None 1/2
is Cleared PC + k + 1
BRIE K Branch if Interrupt if ( I = 1) then PC = None 1/2
Enabled PC + k + 1
BRID K Branch if Interrupt if ( I = 0) then PC = None 1/2
Disabled PC + k + 1
DATA
TRANSFER
INSTRUCTIONS
MOV Rd, Rr Copy Register Rd = Rr None 1
LDI Rd, K Load Immediate Rd = K None 1
LDS Rd, k Load Direct Rd = (k) None 2
LD Rd, X Load Indirect Rd = (X) None 2
LD Rd, X+ Load Indirect and Post- Rd = (X), X = X + 1 None 2
Increment
LD Rd, -X Load Indirect and Pre- X = X - 1, Rd =(X) None 2
Decrement
LD Rd, Y Load Indirect Rd = (Y) None 2
LD Rd, Y+ Load Indirect and Post- Rd = (Y), Y = Y + 1 None 2
Increment
LD Rd, -Y Load Indirect and Pre- Y = Y - 1, Rd = (Y) None 2
Decrement
LDD Rd,Y+q Load Indirect with Rd = (Y + q) None 2
Displacement
LD Rd, Z Load Indirect Rd = (Z) None 2
LD Rd, Z+ Load Indirect and Post- Rd = (Z), Z = Z+1 None 2
Increment
LD Rd, -Z Load Indirect and Pre- Z = Z - 1, Rd = (Z) None 2
Decrement
LDD Rd, Z+q Load Indirect with Rd = (Z + q) None 2
Displacement
STS k, Rr Store Direct (k) = Rr None 2
ST X, Rr Store Indirect (X) = Rr None 2
ST X+, Rr Store Indirect and Post- (X) = Rr, X = X + 1 None 2
Increment
ST -X, Rr Store Indirect and Pre- X = X - 1, (X) = Rr None 2
Decrement
ST Y, Rr Store Indirect (Y) = Rr None 2
ST Y+, Rr Store Indirect and Post- (Y) = Rr, Y = Y + 1 None 2
Increment
ST -Y, Rr Store Indirect and Pre- Y = Y - 1, (Y) = Rr None 2
Decrement
STD Y+q,Rr Store Indirect with (Y + q) = Rr None 2
Displacement
ST Z, Rr Store Indirect (Z) = Rr None 2
ST Z+, Rr Store Indirect and Post- (Z) = Rr, Z = Z + 1 None 2
Increment
ST -Z, Rr Store Indirect and Pre- Z = Z - 1, (Z) = Rr None 2
Decrement
STD Z+q,Rr Store Indirect with (Z + q) = Rr None 2
Displacement
LPM Load Program Memory R0 =(Z) None 3
IN Rd, P In Port Rd = P None 1
OUT P, Rr Out Port P = Rr None 1
PUSH Rr Push Register on Stack STACK = Rr None 2
POP Rd Pop Register from Stack Rd = STACK None 2
BIT AND BIT-
TEST
INSTRUCTIONS
LSL Rd Logical Shift Left Rd(n+1) Z,C,N,V,H 1
=Rd(n),Rd(0)=
0,C=Rd(7)
LSR Rd Logical Shift Right Rd(n) = Rd(n+1), Z,C,N,V 1
Rd(7) =0, C=Rd(0)
ROL Rd Rotate Left Through Rd(0) =C, Rd(n+1) Z,C,N,V,H 1
Carry =Rd(n),C=Rd(7)
ROR Rd Rotate Right Through Rd(7) =C,Rd(n) Z,C,N,V 1
Carry =Rd(n+1),C¬Rd(0)
ASR Rd Arithmetic Shift Right Rd(n) = Rd(n+1), Z,C,N,V 1
n=0..6
SWAP Rd Swap Nibbles Rd(3..0) « Rd(7..4) None 1
BSET S Flag Set SREG(s) = 1 SREG(s) 1
BCLR S Flag Clear SREG(s) = 0 SREG(s) 1
SBI P, b Set Bit in I/O Register I/O(P, b) = 1 None 2
CBI P, b Clear Bit in I/O Register I/O(P, b) = 0 None 2
BST Rr, b Bit Store from Register to T = Rr(b) T 1
T
BLD Rd, b Bit load from T to Rd(b) = T None 1
Register
SEC Set Carry C=1 C 1
CLC Clear Carry C=0 C 1
SEN Set Negative Flag N=1 N 1
CLN Clear Negative Flag N=0 N 1
SEZ Set Zero Flag Z=1 Z 1
CLZ Clear Zero Flag Z=0 Z 1
SEI Global Interrupt Enable I = 1 I 1
CLI Global Interrupt Disable I = 0 I 1
SES Set Signed Test Flag S=1 S 1
CLS Clear Signed Test Flag S=0 S 1
SEV Set Twos Complement V=1 V 1
Overflow
CLV Clear Twos Complement V = 0 V 1
Overflow
SET Set T in SREG T=1 T 1
CLT Clear T in SREG T=0 T 1
SHE Set Half Carry Flag in H=1 H 1
SREG
CLH Clear Half Carry Flag in H = 0 H 1
SREG
NOP No Operation None 1
SLEEP Sleep None 1
WDR Watchdog Reset None 1
Rr: R0-R31
b: Constant (0-7)
s: Constant (0-7)
P: Constant (0-31/63)
K: Constant (0-255)
q: Constant (0-63)
Rdl: R24, R26, R28, R30. For ADIW and SBIW instructions
They produce professional programmers too. This simple programmer you can make yourself within 10
minutes.
What you need is a DB25 centronics male connector, a flat cable and a connector that can be connected to the
target MCU board.
DB25 pin Target MCU Target MCU Target MCU pin 8515 DT104
pin(AT90S8535)
M103/M128
2, D0 MOSI, pin 6 PE.0, 2 MOSI, 6 J5, pin 4
4, D2 RESET, pin 9 RESET, 20 RESET, 9 J5, pin 8
5, D3 CLOCK, pin 8 PB.1,11 CLOCK, 8 J5, pin 6
11, BUSY MISO, pin 7 PE.1, 3 MISO, 7 J5, pin 5
18-25,GND GROUND GROUND GND,20 J5, pin 1
The MCU pin numbers are shown for an 8535! And 8515
You can use a small resistor of 100-220 ohm in series with the D0, D2 and D3 line in order not to short circuit
your LPT port in the event the MCU pins are high.
Tip : when testing programmers etc. on the LPT it is best to buy an I/O card for your PC that has a LPT
port. This way you don’t destroy your LPT port that is on the motherboard in the event you make a mistake!
The following picture shows the connections to make. Both a setup for the DT104 and stand-alone PCB are
shown.
I have been having spurious success with the simple cable programmer from Sample Electronics for the AVR
series.
After resorting to hooking up the CRO I have figured it out (I think). When trying to identify the chip, no
response on the MISO pin indicates that the Programming Enable command has not been correctly received by
the target.
The SCK line Mark/Space times were okay but it looked a bit sad with a slow rise time but a rapid fall time. So
I initially tried to improve the rise
time with a pull-up. No change ie still could not identify chip. I was about to add some buffers when I came
across an Atmel app note for their serial programmer "During this first phase of the programming cycle,
keeping the SCK line free from pulses is critical, as pulses will cause the target AVR to loose synchronization
with the programmer. When synchronization is lost, the only means of regaining synchronization is to release
the RESET line for more than 100ms."
I have added a 100pF cap from SCK to GND and works first time every time now. The SCK rise time is still sad
but there must have been enough noise to corrupt the initial command despite using a 600mm shielded cable.
This page is intended to show the outline of the chip and to provide additional information that might not be
clear from the data sheet.
When you have problems with timing set the right fuse bit A987= 0101. This will solve this problem.
I have just found a small difference in PortB when using the Mega163 in place of a 8535. The difference is in
regard to PortB.4 - PortB.7 when not used as a SPI
interface. The four upper bits of PortB are shared with the hardware SPI unit.
If the SPI is configured in SLAVE mode (DEFAULT) the MOSI , SCK , /SS
The /SS (slave select) pin also has restrictions on it when using it as a general input.- see data sheet ATmega163
- p57.
This sample allows you to use the upper nibble of PortB as outputs.
Portb = &B0000_0000
If The SPCR register is not set for Master, you cannot set the pins for
Output.