вот работающий код, реализующий передачу на LCD температуры и меняющий цвет лампочки в зависимости от этой температуры:
CODE
#include "lcd.h"
#include "leds.h"
#include <lpc21xx.h>
#include <stdio.h>
__irq void I2CISR (void);
void I2CReceiveBytes(void);
void work2 (int temp);
volatile unsigned lock;
volatile int msg1=0, msg2=0;
volatile int I2CAddress;
volatile int bWrite = 1;
volatile unsigned int Counter = 0;
const unsigned char DEGREE = 3;
int main (void)
{
unsigned char result1 [30];
unsigned char result2 [30];
lock = 0;
VICVectCntl1 = 0x00000029; //select a priority slot for a given interrupt
VICVectAddr1 = (unsigned int) I2CISR; //pass the address of the IRQ into the VIC slot
VICIntEnable = 0x00000200; //enable interrupt
PINSEL0 = 0x50; //Switch GPIO to I2C pins
I2SCLH = 0xFF;
I2SCLL = 0xFF;
InitLCD ();
InitSPI();
SetBacklight (1);
//LCDTextOut (" LM75 ", "Individual task ");
InitPWM();
while(1)
{
// bWrite = 1;
I2CReceiveBytes(); //Read the LM75
//LCDTextOut (" ", " "); //Clear LCD
sprintf (result1, "Temp: +%2d%cC ", (int) msg1, DEGREE);
LCDTextOut ("[LM75]: ", result1);
ShowVolume((int) msg1);
//work2((int)msg1);
//Sleep(1000);
}
return 0;
}
__irq void I2CISR (void) //I2C interrupt routine
{
switch (I2STAT) //Read result code and switch to next action
{
case( 0x08 ): //Start bit
if(bWrite==1)
{
I2CONCLR = 0x20; //Clear start bit
I2DAT = (unsigned char)0x90; //Send address and write bit
break;
}
else
{
I2DAT = (unsigned char)0x91; //Send address and read bit
I2CONSET = 0x04; //Set AA bit
I2CONCLR = 0x20; //Clear start bit
// msg1=1;
break;
}
case( 0x10 ): //A repeated Start condition has been transmitted
if(bWrite==1)
{
I2DAT = 0x90; //Send address and read bit
I2CONSET = 0x04; //Set AA bit
I2CONCLR = 0x20; //Clear start bit
}
else
{
I2DAT = 0x91; //Send address and read bit
I2CONSET = 0x04; //Set AA bit
I2CONCLR = 0x20; //Clear start bit
}
// msg1=2;
break;
case( 0x18 ):
I2DAT = (int) 0;
// msg1=I2STAT;
break;
case( 0x28 ):
I2CONSET = 0x00000020; //Start condition
bWrite = 0;
break;
case ( 0x40 ) : //Slave Address +R, ACK
I2CONSET = 0x04; //Enable ACK for data byte
// msg1=3;
break;
case ( 0x48 ) : //Slave Address +R, Not Ack
I2CONSET = 0x20; //Resend Start condition
// msg1=4;
break;
case (0x50) : //Data Received, ACK
msg1 = I2DAT; //Read data byte
Counter+=1;
//msg2=I2STAT;
I2CONCLR = 0x04; //Disable ACK for data byte
// msg1=5;
break;
case (0x58): //Data Received, Not Ack
msg2 = I2DAT; //Read data byte
I2CONSET = 0x10; //Stop condition
lock = 0; //Signal end of I2C activity
// msg1=6;
break;
default :
// msg1=I2STAT;
break;
}
I2CONCLR = 0x08; //Clear I2C interrupt flag
VICVectAddr = 0x00000000; //Clear interrupt in
}
void work2 (int temp){
int nR, nG, nB;
int gR, gG, gB;
switch(temp){
case (25) :
nR = 0, nG = 0, nB = 0xFFF;
break;
case (26) :
nR = 0x1FF, nG = 0, nB = 0x9FF;
break;
case (27) :
nR = 0x3FF, nG = 0, nB = 0x8FF; Beep();
break;
case (28) :
nR = 0x7FF, nG = 0, nB = 0x7FF;
break;
case (29) :
nR = 0x8FF, nG = 0, nB = 0x5FF;
break;
case (30) :
nR = 0x9FF, nG = 0, nB = 0x3FF;
break;
case (31) :
nR = 0xFFF, nG = 0, nB = 0;
break;
default :
nR = 0, nG = 0xFFF, nB = 0;
break;
}
while((gR!=nR) || (gG!=nG) || (gB!=nB))
{
if(gR>nR)
{
gR = gR-1;
}else{
if(gR<nR)
gR = gR+1;
}
if(gG>nG){
gG = gG-1;
}else{
if(gG<nG)
gG = gG+1;
}
if(gB>nB){
gB = gB-1;
}else{
if(gB<nB)
gB = gB+1;
}
// SetRGB (gR, gG, gB);
Sleep (2);
if( (gR==nR) && (gG==nG) && (gB==nB)){
return;
}
}
}
void I2CReceiveBytes(void)
{
while(lock == 1) //Wait for interrupt to signal end of I2C activity
{
;
}
lock = 1; //Set I2C bus as active
// I2ADR = 0; //Place address in Globals to be used by the interrupt
I2CONCLR = 0x000000FF; //Clear all I2C settings
I2CONSET = 0x00000040; //Enable the I2C interface
I2CONSET = 0x00000020; //Start condition
}
Моя задача выглядит так:
по нажатию P0.14 происходит t -> EEPROM в виде 0x00
дальше выключается питание затем включается и происходит следующее:
по UART передать содержимое 0x00..0x20 в виде строк: "содержимое ячейки с адресом 0x00 = ...
0x01=..
0x02=..
и т.д. в гипертерминал.
Сообщение отредактировал kussani - Jun 28 2009, 09:57