ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 12.06.2025
Просмотров: 8268
Скачиваний: 1
© MCS Electronics, 1995-2007
ASCII Table (Incomplete) |
||||
Decimal |
Hex |
Binary |
Value |
|
------- |
--- |
------ |
----- |
(Null char.) |
000 |
000 |
00000000 |
NUL |
|
008 |
008 |
00001000 |
BS |
(Backspace) |
009 |
009 |
00001001 |
HT |
(Horizontal Tab) |
010 |
00A |
00001010 |
LF |
(Line Feed) |
012 |
00C |
00001100 |
FF |
(Form Feed) |
013 |
00D |
00001101 |
CR |
(Carriage Return) |
048 |
030 |
00110000 |
0 |
|
049 |
031 |
00110001 |
1 |
|
052 |
034 |
00110100 |
4 |
|
065 |
041 |
01000001 |
A |
|
066 |
042 |
01000010 |
B |
|
067 |
043 |
01000011 |
C |
|
You can find a complete ASCII table here
CARRIAGE RETURN (CR) AND LINE FEED (LF)
In the previous example you can also see that a second print statement always prints the printed text to the following line. This is caused by the fact that the print statement always adds the CR and LF characters.
Basically if we state:
Print “ABC”
We send 65 66 67 13 10 to the UART. (In binary format)
The carriage return character (13) returns the cursor back to column position 0 of the current line. The line feed (10) moves the cursor to the next line.
Print “ABC” ;
When we type a semicolon ( ; ) at the end of the line...
Bascom does not send a carriage return/line feed, so you can print another text after the ABC on the same line.
Print “ABC” ; Chr(13) ;
This would send only ABC CR. The next print would overwrite the ABC.
OVERVIEW
Here are some other commands that you can use for UART communications:
Waitkey()
Waitkey will until a character is received in the serial buffer.
Ischarwaiting()
Isrcharwaiting returns 1 when a character is waiting in the hardware UART buffer.
Inkey()
Inkey returns the ASCII value of the first character in the serial input buffer.
Sends a variable or non-variable string to the UART
ANOTHER EXAMPLE
page -119-
© MCS Electronics, 1995-2007
This example shows how to use Ischarwaiting to test if there is a key pressed. And if there is, read to a variable.
'Print "Press B key to start"
Dim SerialcharwaitingAs Byte,SerialcharAs Byte
Serialcharwaiting= Ischarwaiting() 'Check if B or b pressed then goto
IfSerialcharwaiting= 1Then
Serialchar= Inkey()
IfSerialchar = 66 Or Serialchar = 98 Then
Goto MyRoutine
End If
End If
Goto Main
Myroutine:
'Statements
Main:
'Statements
End
BUFFERING SERIAL DATA
If you wish to send and receive data at high speed, you need to use serial input and serial output buffers. This buffering is implemented in BASCOM-AVR and can only be used for hardware UART’s.
To configure a UART to use buffers, you need to use the Config statement.
Config Serialout= Buffered ,Size = 20
and/or
Config Serialin= Buffered ,Size = 20
More information can be found in BASCOM-Help. Search topic = "config serialin". There is also a sample program “RS232BUFFER.BAS” in the samples folder if you wish a demonstration of the buffering.
SOFTWARE UART
The previous examples used the hardware UART. That means the compiler uses the internal UART registers and internal hardware (RxD(0) and TxD(0)) of the AVR. If you don’t have a hardware UART you can also use a software UART.
The Bascom compiler makes it easy to “create” additional UART’s. Bascom creates software UART’s on virtually every port pin.
Remember that a software UART is not as robust as a hardware UART, thus you can get timing problems if you have lots of interrupts in your program.
For this example we use microcontroller pins portc.1 and portc.2.
Connect portc.1 to TxD and portc.2 to RxD see the schematic above.
Change the $regfile and program this example:
$regfile= "m88def.dat"
page -120-
© MCS Electronics, 1995-2007
$crystal= 8000000 $baud = 19200
Dim B As Byte
Waitms 100
'Open a TRANSMIT channel for output
Open "comc.1:19200,8,n,1"For OutputAs #1 Print#1 ,"serial output"
'Now open a RECEIVE channel for input Open "comc.2:19200,8,n,1"For InputAs #2
'Since there is no relation between the input and output pin 'there is NO ECHO while keys are typed
Print#1 ,"Press any alpha numerical key"
'With INKEY() we can check if there is data available
'To use it with the software UART you must provide the channel
Do
'Store in byte |
|
B= Inkey(#2) |
|
'When the value > 0 we got something |
|
IfB > 0Then |
'Print the character |
Print#1 ,Chr(b) |
|
End If |
|
Loop |
'Close the channels |
Close#2 |
|
Close#1 |
|
End |
|
After you have programmed the controller and you connected the serial cable, open the
terminal emulator by clicking on in Bascom.
You should see the program asking for an alphanumerical input, and it should print the input back to the terminal.
Using the I²C protocol
I²C bus
I²C bus is an abbreviation for Inter Integrated Circuit bus. It is also known as IIC and I2C.
I²C is a serial and synchronous bus protocol. In standard applications hardware and timing are often the same. The way data is treated on the I²C bus is to be defined by the manufacturer of the I²C master and slave chips.
In a simple I²C system there can only be one master, but multiple slaves. The difference between master and slave is that the master generates the clock pulse. The master also defines when communication should occur. For bus timing it is important that the slowest slave should still be able to follow the master’s clock. In other words the bus is as fast as the slowest slave.
A typical hardware configuration is shown in the figure below:
page -121-
© MCS Electronics, 1995-2007
Note that more slave chips can be connected to the SDA and SCL lines, normally Rp has a value of 1kOHM. The clock generated by the master is called Serial CLock (SCL) and the data is called Serial DAta (SDA).
In most applications the microcontroller is the I²C Master. Slave chips can be Real Time Clocks and Temperature sensors. For example the DS1307 and the DS1624 from www.maxim-ic.com. Of coarse you can also create your own slaves. In that case there is microcontroller to microcontroller communication.
LOGIC BUS LEVELS AND CONDITIONS
Data can only occur after the master generates start condition. A start condition is a high-to-low transition of
the SDA line while SCL remains high. After each data transfer a stop condition is generated. A stop condition is a low-to-high transition of the SDA line while SCL remains high.
As said a data transfer can occur after start condition of the master. The length of data sent over I²C is always 8 bit this includes a read/write direction bit, so you can effectively
page -122-
© MCS Electronics, 1995-2007
send 7 bits every time.
The most significant bit MSB is always passed first on the bus.
If the master writes to the bus the R/W bit = 0 and if the master reads the R/W bit = 1.
After the R/W bit the master should generate one clock period for an acknowledgement ACK.
Each receiving chip that is addressed is obliged to generate an acknowledge after the reception of each byte. A chip that acknowledges must pull down the SDA line during the acknowledge clock pulse in such a way that the SDA line is stable LOW during the HIGH period of the acknowledge related clock pulse.
After an acknowledge there can be a stop condition, if the master wishes to leave the bus idle. Or a repeated start condition. A repeated start is the same as a start condition.
When the master reads from a slave it should acknowledge after each byte received. There are two reasons for the master not to acknowledge. The master sends a not acknowledge if data was not received correctly or if the master wishes the stop receiving.
In other words if the master wishes to stop receiving, it sends a not acknowledge after the last received byte.
The master can stop any communication on the busat any time by sending a stop condition.
BUS ADRESSING
Let’s say we have a slave chip with the address “1101000” and that the master wishes to write to that slave, the slave would then be in receiver mode, like this:
You can see here that the master always generates the start condition, then the master sends the address of the slave and a “0” for R/W. After that the master sends a command or word address. The function of that command or word address can be found in the datasheet of the slave addressed.
After that the master can send the data desired and stop the transfer with a stop condition.
Again the start condition and the slave address, only this time the master sends “1” for the R/W bit. The slave can then begin to send after the acknowledge. If the master wishes to
page -123-
© MCS Electronics, 1995-2007
stop receiving it should send a not acknowledge.
EXAMPLE
This example shows you how to setup and read the temperature from a DS1624 temperature sensor.
Connect the DS1624 like this:
Then program this sample into your microcontroller and connect your microcontroller to the serial port of your PC.
$regfile = "m88def.dat" |
'Define the chip you use |
$crystal = 8000000 |
'Define speed |
$baud = 19200 |
'Define UART BAUD rate |
'Declare RAM for temperature storage |
'Storage for the temperature |
Dim I2ctemp As Byte |
|
'Configure pins we want to use for the I²C bus |
|
Config Scl = Portd.1 |
'Is serial clock SCL |
Config Sda = Portd.3 |
'Is serial data SDA |
'Declare constants - I2C chip addresses |
|
Const Ds1624wr = &B10010000 |
'DS1624 Sensor write |
Const Ds1624rd = &B10010001 |
'DS1624 Sensor read |
'This section initializes the DS1624 |
'Sends start condition |
I2cstart |
|
I2cwbyte Ds1624wr |
'Sends the address |
'byte with r/w 0
'Access the CONFIG register (&HAC address byte)
I2cwbyte &HAC
'Set continuous conversion (&H00 command byte)
I2cwbyte &H00
I2cstop 'Sends stop condition Waitms 25 'We have to wait some time after a stop
page -124-
© MCS Electronics, 1995-2007
I2cstart I2cwbyte Ds1624wr
'Start conversion (&HEE command byte)
I2cwbyte &HEE
I2cstop
Waitms 25
'End of initialization
'Print empty line |
Do
'Get the current temperature
I2cstart I2cwbyte Ds1624wr
I2cwbyte &HAA 'Read temperature (&HAA command byte)
I2cstart
I2cwbyte Ds1624rd 'The chip will give register contents 'Temperature is stored as 12,5 but the ,5 first
I2crbyte I2ctemp
'So you'll have to read twice... first the ,5
I2crbyte I2ctemp , Nack
'And then the 12... we don't store the ,5
I2cstop
'That's why we
read twice.
'We give NACK if the last byte is read
'Finally we print
Print "Temperature: " ; Str(i2ctemp) ; " degrees" ; Chr(13);
Waitms 25
Loop
End
You should be able to read the temperature in your terminal emulator.
Note that the used command bytes in this example can be found in DS1624 temperature sensor datasheet.
OVERVIEW
ConfigSda= Portx.x
Configures a port pin for use as serial data SDA.
ConfigScl= Portx.x
Configures a port pin for use as serial clock SCL.
I2cstart
Sends the start condition.
I2cstop
Sends the stop condition.
I2cwbyte
Writes one byte to an I²Cslave.
page -125-
© MCS Electronics, 1995-2007
I2crbyte
Reads one byte from an I²Cslave.
I2csend
Writes a number of bytes to an I²Cslave.
I2creceive
Reads a number of bytes from an I²Cslave.
Practice
The design below shows how to implement an I2C-bus. The circuit shown is for the 8051 micro the AT89C2051 which is pin compatible with the AT90S2313. It also works for the AVR.
R1 and R2 are 330 ohm resistors.
R3 and R4 are 10 kilo-ohm resistors. For 5V, 4K7 is a good value in combination with AVR chips.
You can select which port pins you want to use for the I2C interface with the com piler settings.
The following information was submitted by Detlef Queck.
Many people have problems over and over with I2C(TWI) Termination. Use 4,7k or 10 k pullup? How long can the SCL, SDA line be when used with pullups etc, etc.
You can simplify this confusing problem. Here is a Schematic for an active Termination of I2C and TWI. We have used this Schematic for over 10 years, and have had no problems with it. The I2C (TWI) lines can be up to 80cm (400KHz) without any problemwhen the Terminator is at the end of the lines.
page -126-