Thursday, 30 June 2016

ARM7 LPC 2148 UART Serial program

Let us see how to write program in ARM

/* ADC Prototypes */
void Adc0Init(unsigned char clkdiv);
unsigned int Adc0Read(unsigned char channel);

/* UART Prototypes */
void Uart0Init (void);
void Uart0PutCh (unsigned char ch);
void  Uart0PutS(unsigned char *str);
void  SendHexData0(unsigned char *str0, int StrLenCntr0);
void Uart1PutCh (unsigned char ch);
unsigned char Uart1GetCh (void);
void  Uart1PutS(unsigned char *str);
void  SendHexData1(unsigned char *str1, int StrLenCntr1);
void _DelayMs(unsigned int count);

/* Variables */
char  stcol, endcol, j ;
//int val1,val2;
int temp,humidity,IR;
char END[1] = {26};
char SendingText1[6] = {'I','0','0','0',0x0D,0x0A};
char SendingText2[6] = {'T','0','0','0',0x0D,0x0A};
char SendingText3[6] = {'H','0','0','0',0x0D,0x0A};

int localADC0Data = 0;
int localADC1Data = 0;
int localADC2Data = 0;
int localADC3Data = 0;
unsigned char str4[]  = {"                 "};
char InChar;
char RecWordsP1[60];
int RecWordsCntrP1 = 0;
char RecChar_Int;
char CurrReChar;
char PrevReChar;
int GGAFound = 0;
char WorkingString[50];
int WorkStrWordCnt = 0;
unsigned char CopyTheSring[40];
int flag1=0;
int flag2=0;
int flag3=0;
int flag4=0;



PINSEL1 |= ANALOG_IN_DIR;
IODIR0 &= ~(ANALOG_IN);
Adc0Init(10);  

  PINSEL1 |=  ANALOG_IN1_DIR;
IODIR0 &= ~(ANALOG_IN1);
Adc0Init(10);

PINSEL1 |= ANALOG_IN2_DIR;
IODIR0 &= ~(ANALOG_IN2);
Adc0Init(10);

 

Uart0Init();

 
while(1)
{
        num1    = Adc0Read(1); //Select channel number
        num2    = Adc0Read(2);
    num3    = Adc0Read(3);
                    //Display Analog voltage on Character L
   IR       = num1;
    temp     = num2/4;
    humidity = num3 ;

 

localADC0Data = IR %1000;
SendingText1[3] = 48+(localADC0Data%10);
localADC0Data = localADC0Data/10;
SendingText1[2] = 48+(localADC0Data%10);
localADC0Data = localADC0Data/10;
SendingText1[1] = 48+(localADC0Data%10);

localADC1Data = temp%1000;
SendingText2[3] = 48+(localADC1Data%10);
localADC1Data = localADC1Data/10;
SendingText2[2] = 48+(localADC1Data%10);
localADC1Data = localADC1Data/10;
SendingText2[1] = 48+(localADC1Data%10);

localADC2Data = humidity%1000;
SendingText3[3] = 48+(localADC2Data%10);
localADC2Data = localADC2Data/10;
SendingText3[2] = 48+(localADC2Data%10);
localADC2Data = localADC2Data/10;
SendingText3[1] = 48+(localADC2Data%10);


 

  if (flag2==0)
  {
flag2=1;
if((IR>500) && (temp>20) && (humidity>50))
   {
    Uart0PutS("AT\r\n");
_DelayMs(100);
Uart0PutS("AT+CMGF=1\r\n");              
_DelayMs(200);
Uart0PutS("AT+CMGS=\"9767034006\"\r\n");  
_DelayMs(200);
SendHexData0(SendingText1, 6);
_DelayMs(200);
Uart0PutS(" Detected\r\n");
_DelayMs(200);
Uart0PutS(CopyTheSring);
_DelayMs(200);
SendHexData0(END,1);

// GPS
for(FillChar = 0; FillChar < 24; FillChar++)
  {
    CopyTheSring[FillChar] = WorkingString[FillChar + 14];
  }
    SendHexData0(CopyTheSring, 24);
_DelayMs(50);
SendHexData0(END, 1);
_DelayMs(50);
   _DelayMs(200);

  }
}
else
{
flag2=0;

}

if (temp>20)
{
  if (flag3==0)
  {
   flag3=1;
Uart0PutS("AT\r\n");
_DelayMs(100);
Uart0PutS("AT+CMGF=1\r\n");              
_DelayMs(200);
Uart0PutS("AT+CMGS=\"9767034006\"\r\n");  
_DelayMs(200);
SendHexData0(SendingText2, 6);
_DelayMs(200);
Uart0PutS("High Temp.\r\n");
_DelayMs(200);
SendHexData0(END,1);

  }
}
else
{
  flag3=0;
}

   if (humidity>50)
{
if (flag4==0)
{ flag4=1;

  Uart0PutS("AT\r\n");
_DelayMs(100);
Uart0PutS("AT+CMGF=1\r\n");              
_DelayMs(200);
Uart0PutS("AT+CMGS=\"9767034006\"\r\n");  
_DelayMs(200);
SendHexData0(SendingText3,6);
_DelayMs(200);
Uart0PutS("High Humidity.\r\n");
_DelayMs(200);
SendHexData0(END,1);

}
}
else
{
  flag4=0;
}

 

}
}

ARM training 

/*ADC Defination*/

 void Adc0Init(unsigned char clkdiv)
{
PCONP |= 0x00001000;                        //Power on the A/D converter 0
//configure the A/D control register of A/D 0
AD0CR =((unsigned long)(clkdiv+1)<<8 ) | _PDN_BIT ;
}

unsigned int Adc0Read(unsigned char channel)
{
unsigned val;
// float AD0=2.5;
AD0CR &= ~(_ADCR_START_MASK|_ADCR_SEL_MASK);  //stop the A/D converter by masking the
   //start bits and channel selection bit    
AD0CR |=((1)<<channel);  //Select the A/D channel
AD0CR |=_ADC0_START;
while(!(AD0GDR & (0x80000000)));    //Wait for the conversion to get over
 //by monitoring the 28th bit of A/D data register
AD0CR &= ~(_ADCR_START_MASK|_ADCR_SEL_MASK);  //Stop the conversion by masking the start bits

val = AD0GDR;
val = ((val>>6 & 0x03FF));  //Extract A/D result
return(val);
}
/*UART Defination*/

void Uart0Init (void)    // Initialize Serial Interface      
{                
  // initialize the serial interface  
   PINSEL0 |= 0x00050005;           // Enable RxD0 and TxD0                    
   U0LCR = 0x83;                   //8 bits, no Parity, 1 Stop bit          
   U0DLL = _9600_15_MHZ;                     //9600 Baud Rate @ 12MHz Clock  
   U0LCR = 0x03;                   //DLAB = 0  

   //Enable Uart FIFO
   U0FCR |= 0x01;

   U1LCR = 0x83;                   // 8 bits, no Parity, 1 Stop bit          
   U1DLL = 97;                     // 9600 Baud Rate @ 12MHz Clock  
   U1LCR = 0x03;                   // DLAB = 0  

   U1FCR |= 0x01;

   // Configure the VIC for Interrupt Handling
   VICVectCntl7 |= (VIC_INT_CH_EN | UART1_CH_NUM);
   VICVectAddr7 = (unsigned int) UART1_ISR;
   VICIntSelect &= (~VIC_UART1_CH);   // optional : you can remove this line : This is to ensure the IRQ Mode
   VICIntEnable |= VIC_UART1_CH;         // Enabel the Interrupt for listening request
   // Enable UART Interrupt

   U1IER   |= 0x01; // DLAB = 0
}




No comments:

Post a Comment