I just need to count the interrupts that occur at pin 3.2 and then print that value in UART. I don't know why the following program is not working as expected. Any help will be appreciated. Thanks in advance.


/* function prototypes */
void delay(unsigned int ms);
void putc1( chr);
void initialize(void);
void record_wind(unsigned char c);
void puts1(char* p);

// global variables declared
unsigned char buf[8]={0},*ptr;
int c=0,i=0; 
sbit P3_2 = P3^2;

/* delay function to create a delay of 1 sec */
void delay(const unsigned int ms)
 unsigned int x;
 unsigned int y;
 for (x = 0; x < ms; x++)
 for (y = 0; y <= 113; y++)

void puts1(char* p)
 char *temp = p;          /*temp pointer so that the actual pointer is not displaced */
 while(*temp != 0x00)

/* function to detect the wind speed */
void windspeed_read( void ) interrupt 0
c = c+1;

/*init the TIMER and etc*/
void initialize()
 SCON  = 0x50;   /*SCON: mode 1, 8-bit UART, enable receive      */ 
 TMOD |= 0x20;   /*TMOD: timer 1, mode 2, 8-bit                  */
 TH1   = 0xFD;   /*TH1:  for 9600 baud                           */
 TR1   = 1;      /*TR1:  timer 1 run                             */ 

/* to print the character in UART and serial window*/
void putc1(chr)
  SBUF = chr;
  while(TI==0);            /*Wait until the character is completely sent */
  TI=0;                   /*Reset the flag */

/* main function */
void main()
IE = 0x81;
EX0 = 1;
EA = 1;

    *ptr = buf;

Above code doesn't count interrupts and it increments in HEX values. I need to increment in decimal(0,1,2,3,..100 or more) and print on UART.

  • \$\begingroup\$ What you mean "not working as expected"? Can you give more information about how it work now? From your code, you tried to send data both in interrupt and in your while loop. \$\endgroup\$ – diverger Dec 6 '14 at 9:57
  • \$\begingroup\$ You should not call initialize() in putc1() - calling initialize() once, at the start of main() should be sufficient. \$\endgroup\$ – Peter Bennett Dec 6 '14 at 18:48
  • \$\begingroup\$ @diverger This code increments in HEX values. I need simple code that counts number of interrupts occurring at Pin 3.2. How to do that ? \$\endgroup\$ – d-coder Dec 8 '14 at 7:49
  • \$\begingroup\$ @PeterBennett Yes! I have removed it. \$\endgroup\$ – d-coder Dec 8 '14 at 7:49

The problem may caused by this code

*ptr = buf;

Look at your definitions for them

unsigned char buf[8]={0},*ptr;

"ptr" is a pointer to character, you should give it a pointer. I think you intend make it point to "buf", then you should use "ptr = buf;", but you give "buf" to "*ptr". That is, "ptr" doesn't point to "buf", indeed it is "0" now, because it's defined in global.


In your code, there are two other problems:

  1. You may send out same counter many times. Because when you've sent out all the chars in the buffer, you have no means to indicate the buffer have been sent, and there always some chars in the buffer. You need a means to make sure your sending starts only when your buffer is updated.
  2. You need access "buf" both in your ISR and the "puts1" function, which is called in the while loop. So, when you are sending the buffer to the USART, your ISR may try to change the contents in "buf", so you may need some synchronization.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.