ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 8347

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

© MCS Electronics, 1995-2007

DDBn Effects on Port B Pins

DDBn

PORTBn

I/O

Pull up

Comment

0

0

Input

No

Tri-state

(Hi-Z)

0

1

Input

Yes

PBn will

source current

if ext. pulled

low.

1

0

Output

No

Push-Pull Zero

Output

1

1

Output

No

Push-Pull One

Output

AVR Internal Hardware Port D

Port D

Port D Pins Alternate Functions

Port

Pin

Alternate Function

PORTD.0

RDX

(UART Input line )

PORTD.1

TDX

(UART Output line)

PORTD.2

INT0

(External interrupt 0 input)

PORTD.3

INT1

(External interrupt 1 input)

PORTD.5

OC1A

(Timer/Counter1 Output compareA match

output)

PORTD.6

WR

(Write strobe to external memory)

PORTD.7

RD

(Read strobe to external memory)

RD - PORTD, Bit 7

RD is the external data memory read control strobe.

WR - PORTD, Bit 6

WR is the external data memory write control strobe.

OC1PORTD, Bit 5

Output compare match output: The PD5 pin can serve as an externaloutput when the Timer/Counter1 com-pare matches.

The PD5 pin has to be configured as an out-put (DDD5 set (one)) to serve this f unction. See the Timer/Counter1 description for further details, and how to enable the output. The OC1 pin is also the output pin for the PWM mode timer function.

page -110-


© MCS Electronics, 1995-2007

INT1 - PORTD, Bit 3

External Interrupt source 1: The PD3 pin can serve as an external interrupt source to the MCU. See the interrupt description for further details, and how to enable the source

INT0 - PORTD, Bit 2

INT0, External Interrupt source 0: The PD2 pin can serve as an externalinterrupt source to the MCU. See the interrupt description for further details, and how to enable the source.

TXD - PORTD, Bit 1

Transmit Data (Data output pin for the UART). When the UART transmitter is enabled, this pin is configured as an output regardless of the value of DDRD1.

RXD - PORTD, Bit 0

Receive Data (Data input pin for the UART). When the UART receiver is enabled this pin is configured as an output regardless of the value of DDRD0. When the UART forces this pin to be an input, a logical one in PORTD0 will turn on the internal pull-up.

When pins TXD and RXD are not used for RS-232 they can be used as an input or output pin.

No PRINT, INPUT or other RS-232 statement may be used in that case.

The UCR register will by default not set bits 3 and 4 that enable the TXD and RXD pins for RS-232 communication. It is however reported that this not works for all chips. In this case you must clear the bits in the UCR register with the following statements:

RESET UCR.3

RESET UCR.4

Adding XRAM

Some AVR chips like the 90S8515 for example can be extended with external RAM (SRAM) memory.

On these chips Port A serves as a Multiplexed Address (A0 – A7)/Data (D0 – D7) bus. Port C also serves as the upper Address bits (A8 - A15) output when using external SRAM.

The maximum size of XRAM can be 64 KBytes.

Example: The STK200 has a 62256 ram chip (32K x 8 bit).

Here is some info from the BASCOM user list :

If you do go with the external ram , be careful of the clock speed.

Using a 4 Mhz crystal , will require a SRAM with 70 nS access time or less. Also the data latch (74HC573) will have to be from a faster

family such as a 74FHC573 if you go beyond 4 Mhz.

You can also program an extra wait state, to use slower memory.

page -111-

© MCS Electronics, 1995-2007

Here you will find a pdf file showing the STK200 schematics:

http://www.avr-forum.com/Stk200_schematic.pdf

If you use a 32 KB SRAM, then connect the /CS signal to A15 which give to the range of &H0000 to &H7FFF, if you use a 64 KB SRAM, then

tie /CS to GND, so the RAM is selected all the time.

Attaching an LCD Display

A LCD display can be connected with two methods.

By wiring the LCD-pins to the processor port pins. This is the pin mode. The advantage is that you can choose the pins and that they don't have to be on the same port. This can make your PCB design simple. The disadvantage is that more code is needed.

By attaching the LCD-data pins to the data bus. This is convenient when you have an external RAM chip and will add only a little extra code.

The LCD-display can be connected in PIN mode as follows:

LCD

PORT

PIN

DISPLAY

DB7

PORTB.7

14

DB6

PORTB.6

13

page -112-


© MCS Electronics, 1995-2007

DB5

PORTB.5

12

DB4

PORTB.4

11

E

PORTB.3

6

RS

PORTB.2

4

RW

Ground

5

Vss

Ground

1

Vdd

+5 Volt

2

Vo

0-5 Volt

3

This leaves PORTB.1 and PORTB.0 and PORTD for other purposes.

You can change these pin settings from the Options LCD menu.

BASCOM supports many statements to control the LCD-display.

For those who want to have more control of the example below shows how to use the internal BASCOM routines.

$ASM

Ldi _temp1, 5

'load register R24 with value

Rcall _Lcd_control

'it is a control value to control the display

Ldi _temp1,65

'load register with new value (letter A)

Rcall _Write_lcd

'write it to the LCD-display

$END ASM

Note that _lcd_control and _write_lcd are assembler subroutines which can be called from BASCOM.

See the manufacturer's details from your LCD display for the correct pin assignment.

Memory usage

SRAM

Every variable uses memory. This memory is also called SRAM.

The available memory depends on the chip.

A special kind of memory are the registers in the AVR. Registers 0-31 have addresses 0-31. Almost all registers are used by the compiler or might be used in the future.

Which registers are used depends on the programstatements you use.

This brings us back to the SRAM.

No SRAM is used by the compiler other than the space needed for the software stackand frame.

Some statements might use some SRAM. When this is the case it is mentioned in the help topic of that statement.

Each 8 bits used occupy one byte.

Each byte variable occupies one byte.

page -113-

© MCS Electronics, 1995-2007

Each integer/word variable occupies two bytes. Each Long or Single variable occupies four bytes. Each double variable occupies 8 bytes.

Each string variable occupies at least 2 byes.

A string with a length of 10. occupies 11 byes. The extra byte is needed to indicate the end of the string.

Use bits or byte variables whereever you can to save memory. (not allowed for negative values)

The software stack is used to store the addresses of LOCAL variables and for variables that are passed to SUB routines.

Each LOCAL variable and passed variable to a SUB, uses two bytes to store the address. So when you have a SUB routine in your program that passes 10 variables, you need 10 * 2 = 20 bytes. When you use 2 LOCAL variables in the SUB program that receives the 10 variables, you need additional 2 * 2 = 4 bytes.

The software stack size can be calculated by taking the maximum number of parameters in a SUB routine, adding the number of LOCAL variables and multiplying the result by 2. To be safe, add 4 more bytes for internally used LOCAL variables.

LOCAL variables are stored in a place that is named the Frame.

When you have a LOCAL STRING with a size of 40 bytes, and a LOCAL LONG, you need 41 + 4 bytes = 45 bytes of frame space.

When you use conversion routines such as STR(), VAL() etc. that convert fromnumeric to string and vice versa, you also need a frame. It should be 16 bytes in this case.

Add additional space for the local data.

Note that the use of the INPUT statement with a numeric variable, or the use of the PRINT or LCD statement with a numeric variable, will also force you to reserve 16 bytes of frame space. This because these routines use the internal numeric<>string conversion routines.

XRAM

You can easy add external memory to an 8515. Then XRAM (extended memory) will become available. When you add a 32 KB RAM, the first address will be 0.

But because the XRAM can only start after the internal SRAM, which is &H0260 for the 8515, the lower memory locations of the XRAM will not be available for use.

ERAM

Most AVR chips have internal EEPROM on board.

This EEPROM can be used to store and retrieve data.

In BASCOM, this data space is called ERAM.

An important difference is that an ERAM variable can only be written to a maximum of 100.000 times. So only assign an ERAM variable when it is needed, and never use it in a loop or the ERAM will become unusable.

Constant code usage

page -114-


© MCS Electronics, 1995-2007

Constants are stored in a constant table.

Each used constant in your program will end up in the constant table.

For example:

Print "ABCD"

Print "ABCD"

This example will only store one constant (ABCD).

Print "ABCD"

Print "ABC"

In this example, two constants will be stored because the strings differ.

UART

UART

A Universal Asynchronous Receiver and Transmitter (UART) can be used to send and receive data between two devices. More specific these devices can be PC-to-PC, PC-to-microcontroller and microcontroller-to-microcontroller. The UART communicates using TTL voltages +5V and 0V or LVTTL depending on your microcontrollers VCC voltage.

If you wish to connect to a PC you need to use RS232 protocol specifications. This means that the hardware communication is done with sapecific voltage levels. (+15V and -15V) This can be achieved by using a MAX232 level shifter.

The hardware is explained in this schematic:

The DB-9 connector has 9 pins but you only need to use 3 of them. Notice that the drawing above shows the FRONT VIEW thus remember that you are soldering on the other side. On most connectors the pin outs can also be found on the connector itself.

If your controller has no UART you can use a software UART see below. If your controller has one UART you connect controller pins TxD and RxD to TxD and RxD in the schematic above. If your controller has more than one UART you connect controller pins TxD0 and

page -115-

© MCS Electronics, 1995-2007

RxD0 to TxD and RxD in the schematic above.

You now need to initialize the program in your microcontroller, open a new .bas file and add the following code in the beginning of your program.

$regfile= "your micro here def.dat" $crystal= 8000000

$baud = 19200

Make sure to define your microcontroller after $regfile for example if you use the ATMega32 $regfile= "m32def.dat"

Some new chips can use an internal oscillator, also some chips are configured to use the internal oscillator by default. Using an internal oscillator means you do not need an external crystal.

Perform this step only if you have an internal oscillator.

Open the BASCOM-AVR programmer like this:

Select the “Lock and Fuse Bits” tab and maximize the programmer window.

Check if you see the following in the “Fusebit” section:

"1:Devide Clock by 8 Disabled" and

"Int. RC Osc. 8 MHz; Start-up time: X CK + X ms; [CKSEL=XXXX SUT=XX]"

These options are not available for all AVR’s, if you don’t have the option do not change any fusebits.

page -116-

© MCS Electronics, 1995-2007

If these options are available, but in a wrong setting. Change the setting in the drop down box and click another Fuse section. Finally click the "Program FS" button. Click "Refresh" to see the actual setting.

Now connect a straight cable between the DB-9 connector, microcontroller side and the PC side.

Program a test program into your microcontroller, it should look like this:

$regfile= "m32def.dat" 'Define your own $crystal= 8000000

$baud = 19200

Do

Print"Hello World"

Waitms 25

Loop

End

Now open the BASCOM-AVR Terminal and set your connection settings by clicking “Terminal” -> “Settings” Select your computers COM port and select baud 19200, Parity none, Databits 8, Stopbits 1, Handshake none, emulation none.

If you see the Hello World displayed in the BASCOM-AVR Terminal emulator window, your configuration is OK. Congratulations.

Example

You can also try this example with the BASCOM Terminal emulator, it shows you how to send and receive with various commands.

$regfile= "m88def.dat" $crystal= 8000000

page -117-


© MCS Electronics, 1995-2007

$baud = 19200

Dim Akey As Byte 'Here we declare a byte variable

Print

Print"Hello, hit any alphanumerical key..."

Akey = Waitkey() 'Waitkey waits untill a char is received from the UART

Print Akey

Wait 1

Print

Print"Thanks!, as you could see the controller prints a number"

Print"but not the key you pressed."

Wait 1

Print

Print"Now try the enter key..."

Akey = Waitkey()

Akey = Waitkey()

Print Akey

Print

Print"The number you see is the ASCII value of the key you pressed."

Print"We need to convert the number back to the key..."

Print 'Notice what this line does

Print"Please try an alphanumerical key again..."

Akey = Waitkey()

PrintChr(akey)'Notice what this does

Print"That's fine!"

Wait 1

Print

Print"For a lot of functions, just one key is not enough..."

Print"Now type your name and hit enter to confirm"

Dim InputstringAs String*12

'Declare a string variable here

Do

Akey = Waitkey()

'On enter key goto thanks

IfAkey = 13 Then Goto Thanks

Inputstring= Inputstring+ Chr(akey)'Assign the string

Loop

Thanks:

'Notice what ; does

Print"Thank you ";Inputstring;"!"

Wait 1

Print

Print"Take a look at the program code and try to understand"

Print"how this program works. Also press F1 at the statements"

Print

Print"If you understand everything continue to the next experiment"

End

ASCII

As you could have seen in the previous example we use the PRINT statement to send something to the UART. Actually we do not send just text. We send ASCII characters. ASCII means American Standard Code for Information Interchange. Basically ASCII is a list of 127 characters.

page -118-