Measuring the IO mouth slightly smaller than the output voltage of 3.3 V
CodePudding user response:
Attach a program//ICC AVR application builder: the 2009-3-12 17:05:04
//Target: M8
//Crystal: 8.0000 Mhz
#include
#include
#include
# include "usart. H"
Void port_init (void)
{
DDRB |=0 x04;//B2, LED_DOWN, PIN16
DDRC |=0 x02;//C1, LED_LEFT, PIN24
DDRC |=0 x08;//C3, LED_RIGHT, PIN26
DDRC |=0 x20;//C5, LED_UP, PIN28
DDRB |=0 x01;//B0, LED_OK, PIN14
DDRD |=0 x40;//D6, TEST, PIN12
DDRC&=~ 0 x01;//C0, KEY_LEFT, PIN23
DDRC&=~ 0 x04;//C2, KEY_RIGHT, PIN25
DDRC&=0 x10 ~;//C4, KEY_UP, PIN27
DDRB&=~ 0 x02;//B1, KEY_DOWN, PIN15
DDRD&=~ 0 x80.//D7, KEY_OK, PIN13
PORTC |=0 XFF;
PORTB |=0 XFF;
PORTD |=0 XFF;
}
//call this routine to initialize all peripherals
Void init_devices (void)
{
//stop errant interrupts until the set up
Cli ();//disable all interrupts
Port_init ();
MCUCR=0 x00;//MCU Control Register
GICR=0 x00;//the General Interrupt Control Register
TIMSK=0 x00;//the timer interrupt sources
Sei ();//re - enable interrupts
//all peripherals are now an initialized
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Use: iic to send and receive
Taget: mega8
Crystal: 8 m
Says: PC4 - SDA
PC5 - SCK/SCL
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
I2C state definition
MT main way transmission MR main way accept
* * * * * * * * * * * * * * * * * * * * * * * * * * */
# define START 0 x08
# define RE_START 0 x10
# define MT_SLA_ACK 0 x18
# define MT_SLA_NOACK 0 x20
# define MT_DATA_ACK 0 x28
# define MT_DATA_NOACK 0 x30
# define MR_SLA_ACK 0 x40
# define MR_SLA_NOACK 0 x48
# define MR_DATA_ACK 0 x50
# define MR_DATA_NOACK 0 x58
# define RD_DEVICE_ADDR 0 x3c//before four fixed device, after three see attachment, the last one is reading and writing instruction
# define WD_DEVICE_ADDR 0 x3d
# define the Start () (TWCR=(1 & lt;
# define SetAck (TWCR |=(1 & lt;
Void delay_ms (int time)
{
int i;
For (; Time> 0; Time -)
for(i=0; i<1000; I++);
}
//initialize
Void iic_init ()
{
TWBR=0 x20;//set the baud rate
TWSR=0 x00;//set the preassigned frequency than
TWCR=0 x44;//can make response, can make the TWI
DDRC |=(1 & lt;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The I2C bus to write one byte
Return 0: write success
Return 1: failed to write
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Unsigned char iic_write (unsigned char Wdata, unsigned char RegAddress)
{
Start ().//I2C start
Wait ();
If (TestAck ()!=START)
return 1;//ACK
X11 Send_Char (0);//test
Write8Bit (WD_DEVICE_ADDR);//write I2C from device address, and write
Wait ();
Send_Char (0 x22);//test
If (TestAck ()!=MT_SLA_ACK)
return 1;//ACK
Send_Char (0 x33);//test
Write8Bit (RegAddress);//write device corresponding register address
Wait ();
If (TestAck ()!=MT_DATA_ACK)
return 1;//ACK
Write8Bit (Wdata);//write data to the device corresponding register
Wait ();
If (TestAck ()!=MT_DATA_ACK)
return 1;//ACK
Stop ();//I2C stop
Delay_ms (100);//delay
return 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
I2C bus read a byte
Returns non-zero: read success
Return 0: failed to read
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Unsigned char iic_read (unsigned RegAddress)
{
Unsigned char temp.
Start ().//I2C start
Wait ();
If (TestAck ()!=START)
return 1;//ACK
Write8Bit (WD_DEVICE_ADDR);//write I2C from device address, and write
Wait ();
If (TestAck ()!=MT_SLA_ACK)
return 1;//ACK
Write8Bit (RegAddress);//write device corresponding register address
Wait ();
If (TestAck ()!=MT_DATA_ACK)
return 1;
Start ().//I2C restart
Wait ();
If (TestAck ()!=RE_START)
return 1;
Write8Bit (RD_DEVICE_ADDR);//write I2C read from the device address and way
Wait ();
If (TestAck ()!=MR_SLA_ACK)
return 1;//ACK
Twi ();//start the main way of I2C read
Wait ();
If (TestAck ()!=MR_DATA_NOACK)
return 1;//ACK
Temp=TWDR;//read the I2C receiving data
Stop ();//I2C stop
Return temp.
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Void main ()
{
Unsigned char I, j;
Port_init ();
Init_devices ();
Usart_Init ();
Iic_init ();
While (1)
{
;
}
}
CodePudding user response:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull