Hello,

I wrote my first little mcs51 program today, for the Cypress FX2LP. But
I encountered a problem: Timer 1 runs too fast. When I set up Timer 0
and Timer 1 the same, Timer 1 runs about 4 times faster.

I first noticed the problem when trying to do a software UART, which
worked with Tiemr 1, but not with Timer 2.

I have attached programs for a 2-bit counter on the LEDs of my board a
bits 0 and 1 of port A. With Timer 1, the counter increments once per
second.

I compile my programs using
sdcc -mmcs51 test2.c
and put them onto the FX2LP using
cycfx2prog prg:test2.ihx run

Philipp
#include <stdbool.h>

__sfr __at(0x80) IOA;
__sfr __at(0xb2) OEA;

__sfr __at(0x88) TCON;
__sfr __at(0x89) TMOD;
__sfr __at(0x8b) TL1;
__sfr __at(0x8d) TH1;

__sfr __at(0xa8) IE;

volatile unsigned long int clocktime;
volatile _Bool clockupdate;

void clockinc(void) __interrupt(3)
{
	TH1 = (65536 - 1000) / 256;
	TL1 = (65536 - 1000) % 256;
	clocktime++;
	clockupdate = true;
}

unsigned long int clock(void)
{
	unsigned long int ctmp;

	do
	{
		clockupdate = false;
		ctmp = clocktime;
	} while (clockupdate);
	
	return(ctmp);
}

void main(void)
{
	// Configure timer
	// 1000 ticks per second
	TH1 = (65536 - 1000) / 256;
	TL1 = (65536 - 1000) % 256;
	TMOD = 0x04;
	IE |= 0x88;
	TCON |= 0x40; // Start timer

	OEA=0x03; // port A as output

	for(;;)
		IOA = ~(clock() / 1000) & 0x03;
}

#include <stdbool.h>

__sfr __at(0x80) IOA;
__sfr __at(0xb2) OEA;

__sfr __at(0x88) TCON;
__sfr __at(0x89) TMOD;
__sfr __at(0x8a) TL0;
__sfr __at(0x8c) TH0;

__sfr __at(0xa8) IE;

volatile unsigned long int clocktime;
volatile _Bool clockupdate;

void clockinc(void) __interrupt(1)
{
	TH0 = (65536 - 1000) / 256;
	TL0 = (65536 - 1000) % 256;
	clocktime++;
	clockupdate = true;
}

unsigned long int clock(void)
{
	unsigned long int ctmp;

	do
	{
		clockupdate = false;
		ctmp = clocktime;
	} while (clockupdate);
	
	return(ctmp);
}

void main(void)
{
	// Configure timer
	// 1000 ticks per second
	TH0 = (65536 - 1000) / 256;
	TL0 = (65536 - 1000) % 256;
	TMOD = 0x01;
	IE |= 0x82;
	TCON |= 0x10; // Start timer

	OEA=0x03; // port A as output

	for(;;)
		IOA = ~(clock() / 1000) & 0x03;
}

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are 
consuming the most bandwidth. Provides multi-vendor support for NetFlow, 
J-Flow, sFlow and other flows. Make informed decisions using capacity 
planning reports. http://sdm.link/zohodev2dev
_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to