Tuesday, 6 October 2015

TIMER/COUNTER0 MODULE OF ATMEGA32L

Timer/Counter0 is a general purpose, single compare unit, 8-bit Timer/Counter module. The
main features are:
• Single Compare Unit Counter
• Clear Timer on Compare Match (Auto Reload)
• Glitch-free, Phase Correct Pulse Width Modulator (PWM)
• Frequency Generator
• External Event Counter
• 10-bit Clock Prescaler
• Overflow and Compare Match Interrupt Sources (TOV0 and OCF0)
The Timer/Counter (TCNT0) and Output Compare Register (OCR0) are 8-bit registers. Interrupt
request (abbreviated to Int.Req. in the figure) signals are all visible in the Timer Interrupt Flag
Register (TIFR). All interrupts are individually masked with the Timer Interrupt Mask Register
(TIMSK). TIFR and TIMSK are not shown in the figure since these registers are shared by other
timer units.
The Timer/Counter can be clocked internally, via the prescaler, or by an external clock source on
the T0 pin. The Clock Select logic block controls which clock source and edge the Timer/Counter
uses to increment (or decrement) its value. The Timer/Counter is inactive when no clock source
is selected. The output from the Clock Select logic is referred to as the timer clock (clkT0).
The double buffered Output Compare Register (OCR0) is compared with the Timer/Counter
value at all times. The result of the compare can be used by the waveform generator to generate
a PWM or variable frequency output on the Output Compare Pin (OC0).The compare match event will also set the Compare Flag (OCF0) which can be used to generate an output compare interrupt request.

Tuesday, 29 September 2015

TIMER2 MODULE OF PIC16F877A

Timer2 is an 8-bit timer with a prescaler and a postscaler. It can be used as the PWM time base for the PWM mode of the CCP module(s). The TMR2 register is readable and writable and is cleared on any device Reset. The input clock (FOSC/4) has a prescale option of 1:1, 1:4 or 1:16, selected by control bits T2CKPS1:T2CKPS0 (T2CON<1:0>).The Timer2 module has an 8-bit period register, PR2.Timer2 increments from 00h until it matches PR2 and then resets to 00h on the next increment cycle. PR2 is a readable and writable register. The PR2 register is initialized to FFh upon Reset.
The match output of TMR2 goes through a 4-bit postscaler (which gives a 1:1 to 1:16 scaling inclusive) to generate a TMR2 interrupt (latched in flag bit, TMR2IF (PIR1<1>)). Timer2 can be shut-off by clearing control bit, TMR2ON (T2CON<2>), to minimize power consumption.

Program to Generate a 1s delay ....

#include<pic.h>
void timer();
void main()
{
TRISD=0X00;
T2CON=0X7E;
while(1)
{
PORTD=0XFF;
timer();
PORTD=0X00;
timer();
}
}
void timer()
{
int i;
TMR2=0X00;
for(i=0;i<15;i++)
{
while(TMR2IF==0);
TMR2IF=0;
}
}

Generate a 1s delay using TIMER1 in PIC16F877A

#include<pic.h>
void timer();
void main()
{
TRISD=0X00;
T1CON=0X39;
while(1)
{
PORTD=0XFF;
timer();
PORTD=0X00;
timer();
}
}
void timer()
{
int i;
TMR1L=0X00;
TMR1H=0X00;
for(i=0;i<2;i++)
{
while(TMR1IF==0);
TMR1IF=0;
}
}

TIMER1 MODULE OF PIC16F877A

The Timer1 module is a 16-bit timer/counter consisting of two 8-bit registers (TMR1H and TMR1L) which are readable and writable. The TMR1 register pair (TMR1H:TMR1L) increments from 0000h to FFFFh and rolls over to 0000h. The TMR1 interrupt, if enabled, is generated on overflow which is latched in interrupt flag bit, TMR1IF (PIR1<0>). This interrupt can be enabled/disabled by setting/clearing TMR1 interrupt enable bit, TMR1IE (PIE1<0>). Timer1 can operate in one of two modes:
• As a Timer
• As a Counter
The operating mode is determined by the clock select bit, TMR1CS (T1CON<1>).In Timer mode, Timer1 increments every instruction cycle. In Counter mode, it increments on every rising
edge of the external clock input. Timer1 can be enabled/disabled by setting/clearing
control bit, TMR1ON (T1CON<0>). Timer1 also has an internal “Reset input”. This Reset
can be generated by either of the two CCP modules.When the Timer1 oscillator is enabled (T1OSCEN is set), the RC1/T1OSI/CCP2 and RC0/T1OSO/T1CKI pins become inputs. That is, the TRISC<1:0> value is ignored and these pins read as ‘0’. Additional information on timer modules is available in the PICmicro® Mid-Range MCU Family Reference Manual (DS33023).

Digital Clock using Timer0 in PIC16F877A

#include<pic.h>
void print(int);
void delay();
void dela();
void cmd(int);
void data(char);
char keypad();
void main()
{
int i,j=0,k=0,l=0,o=0;
int a,b,c,d,e,f;
OPTION=0X07;
TRISB=0X00;
TRISC=0X00;
TRISD=0X0F;
cmd(0x38);
cmd(0X0E);
cmd(0X06);
cmd(0X01);
cmd(0X80);
delay();
a=keypad();
data(a);
delay();
b=keypad();
data(b);
delay();
data(':');
c=keypad();
data(c);
delay();
d=keypad();
data(d);
delay();
data(':');
e=keypad();

data(e);
delay();
f=keypad();
data(f);
j=(e-48)*10+(f-48);
k=(c-48)*10+(d-48);
l=(a-48)*10+(b-48);

while(1)
{
TMR0=0X00;
for(i=0;i<15;i++)
{
while(TMR0IF==0);
TMR0IF=0;
}
cmd(0X80);
j++;
if(j>59)
{
j=0;
k++;
}
if(k>59)
{
k=0;
l++;
}
if(l>11)
{
l=0;
cmd(0X01);
o++;
}
print(l);
data(':');
print(k);
data(':');
print(j);
if(o%2==0)
{
data(' ');
data(' ');
data('A');
data('M');
}
else
{
data(' ');
data(' ');
data('P');
data('M');
}
}
}
void print(int h)
{
int c,p,a[2],q;
if(h==0)
data(0+48);
for(p=0;h>0;p++)
{
c=h%10;
h=h/10;
a[p]=c;
}
for(q=p-1;q>=0;q--)
data(a[q]+48);
}
void cmd(int s)
{
PORTB=s;
RC0=0;
RC1=1;
dela();
RC1=0;
}

void data(char a)
{
PORTB=a;
RC0=1;
RC1=1;
dela();
RC1=0;
}
void dela()
{int i,j;
for(i=0;i<100;i++)
for(j=0;j<100;j++);
}
void delay()
{int i,j;
for(i=0;i<100;i++)
for(j=0;j<1000;j++);
}
char keypad()
{
int e;
PORTD=0X7F;
e=PORTD&0X0F;
switch(e)
{
case 0X07:
return '1';
break;
case 0X0B:
return '2';
break;
case 0X0D:
return '3';
break;
case 0X0E:
return('4');
break;
}
PORTD=0XBF;
e=PORTD&0X0F;
switch(e)
{
case 0X07:
return('4');
break;
case 0X0B:
return('5');
break;
case 0X0D:
return('6');
break;
case 0X0E:
return('7');
break;
}
PORTD=0XDF;
e=PORTD&0X0F;
switch(e)
{
case 0X07:
return('8');
break;
case 0X0B:
return('9');
break;
case 0X0D:
return('0');
break;
case 0X0E:
return('A');
break;
}
PORTD=0XEF;
e=PORTD&0X0F;
switch(e)
{
case 0X07:
return('B');
break;
case 0X0B:
return('C');
break;
case 0X0D:
return('D');
break;
case 0X0E:
return('E');
break;
}
}

Program to Blink PORTC when TIMER0 overflows....



#include<pic.h>
void delay(int);
void main()
{
TRISC=0X00;
OPTION=0X07;
TMR0=0X00; 
while(TMR0IF==0);
while(1)
{
PORTC=0XFF;
delay(10);
PORTC=0X00;
delay(10);
}
}
void delay(int k)
{
int j,i;
for(i=0;i<k;i++)
for(j=0;j<1000;j++);
}

TIMER0 MODULE OF PIC16F877A

The Timer0 module timer/counter has the following
features:
• 8-bit timer/counter
• Readable and writable
• 8-bit software programmable prescaler
• Internal or external clock select
• Interrupt on overflow from FFh to 00h
• Edge select for external clock





Figure  is a block diagram of the Timer0 module andthe prescaler shared with the WDT.
Additional information on the Timer0 module is available in the PICmicro® Mid-Range MCU Family
Reference Manual (DS33023). Timer mode is selected by clearing bit T0CS (OPTION_REG<5>). In Timer mode, the Timer0 module will increment every instruction cycle (without prescaler). If the TMR0 register is written, the increment is inhibited for the following two instruction cycles.
The user can work around this by writing an adjusted value to the TMR0 register.Counter mode is selected by setting bit T0CS (OPTION_REG<5>). In Counter mode, Timer0 will increment either on every rising or falling edge of pin RA4/T0CKI. The incrementing edge is determined by the Timer0 Source Edge Select bit, T0SE (OPTION_REG<4>). Clearing bit T0SE selects the rising edge.

UART TRANSMITTER IN ARM7(LPC2129/38)

#include<lpc21xx.h>
int main()
{

PINSEL0=0X00000001;
U0LCR=0X83;
U0DLL=0X51;
U0DLM=0X00;
U0LCR=0X03;
U0THR='A';
while((U0LSR&0X20)==0);
while(1);
}

UART RECEIVER IN ARM7(LPC2129/38)

#include<lpc21xx.h>
void cmd(int);
void cmd1(int);
void data(char);
void delay();
int main()
{
char s;
PINSEL0=0X00000004;
IO1DIR=0XFFFFFFFF;
cmd(0x30);
cmd(0x30);
cmd(0x30);
cmd(0x20);
cmd1(0x28);
cmd1(0x01);
cmd1(0x06);
cmd1(0x0e);
cmd1(0x80);

U0LCR=0X83;
U0DLL=0X51;
U0DLM=0X00;
U0LCR=0X03;

while(1)
{
while((U0LSR&0X01)==0);
s=U0RBR;
data(s);
 }
 }

 void cmd(int b)
{
unsigned int s;
s=b&0xf0;
IO1SET=s<<16;
IO1CLR=0X000F0000;
IO1SET|=0X00080000;
delay();
IO1CLR=0X00080000;
IO1CLR=0XFFFFFFFF;
}
void cmd1(int a)
{


unsigned int s;
s=a&0xf0;
IO1SET=s<<16;
IO1CLR=0X000F0000;
IO1SET|=0X00080000;
delay();
IO1CLR=0X00080000;
IO1CLR=0XFFFFFFFF;


s=a&0x0f;
IO1SET=s<<20;
IO1CLR=0X000F0000;
IO1SET|=0X00080000;
delay();
IO1CLR=0X00080000;
delay();
IO1CLR=0XFFFFFFFF;
}
void data(char a)
{

char s;
s=a&0xf0;
IO1SET=s<<16;
IO1CLR=0X000F0000;
IO1SET|=0X000A0000;
delay();
IO1CLR=0X00080000;
IO1CLR=0XFFFFFFFF;


s=a&0x0f;
IO1SET=s<<20;
IO1CLR=0X000F0000;
IO1SET|=0X000A0000;
delay();
IO1CLR=0X00080000;
delay();
IO1CLR=0XFFFFFFFF;
}
void delay()
{
int i;
for(i=0;i<10000;i++);
}

Saturday, 26 September 2015

UART : ARM7(LPC2129/38)


The LPC2129/38  each contain two UARTs. In addition to standard transmit and receive data lines, the UART1 also provides a full modem control handshake interface.

Features
 • 16 B Receive and Transmit FIFOs.
 • Register locations conform to 16C550 industry standard.
 • Receiver FIFO trigger points at 1 B, 4 B, 8 B, and 14 B.
 • Built-in fractional baud rate generator covering wide range of baud rates without a need for external      crystals of particular values.
 • Transmission FIFO control enables implementation of software (XON/XOFF) flow control on both    UARTs
 • UART1 is equipped with standard modem interface signals. This module also provides full support    for hardware flow control (auto-CTS/RTS).