Thread: 8051 micro controller programming

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    2

    8051 micro controller programming

    An Application software requires an accurate 0.25 second delay function
    to flash an LED. Show how you would program the C8051F020 to
    generate such a delay using interrupts.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    A student needs to do his homework. Show us some effort.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    2
    kk.... sory .. i have done it with timer 2 ... bt i dont know how to specify the time delay... :-)

    prog:

    Code:
    
    //-- Filename : Lab3A.c
    //-- This program makes the 4 green LEDs (on P5.4 to P5.7) blink (all together)
    //-- Uses Timer 2 and interrupts to control the blinking (on/off) period.
    //-- (Using Timer 4 is very similar to Timer 2)
    //-- Uses 2 MHz internal Oscillator (default)
    
    #include <c8051f020.h>			  // SFR declarations
    
    //------------------------------------------------------------------------------------
    // 16-bit SFR Definitions for 'F02x
    //------------------------------------------------------------------------------------
    
    sfr16 DP     = 0x82;            // data pointer
    sfr16 TMR3RL = 0x92;            // Timer3 reload value
    sfr16 TMR3   = 0x94;            // Timer3 counter
    sfr16 ADC0   = 0xBE;            // ADC0 data
    sfr16 ADC0GT = 0xC4;            // ADC0 greater than window
    sfr16 ADC0LT = 0xC6;            // ADC0 less than window
    sfr16 RCAP2  = 0xCA;            // Timer2 capture/reload
    sfr16 T2     = 0xCC;            // Timer2
    sfr16 RCAP4  = 0xE4;            // Timer4 capture/reload
    sfr16 T4     = 0xF4;            // Timer4
    sfr16 DAC0   = 0xD2;            // DAC0 data
    sfr16 DAC1   = 0xD5;            // DAC1 data
    
    enum {NORMAL, HIGH};
    unsigned char count;
    
    //------------------------------------------------------------------------------------
    // Function PROTOTYPES
    //------------------------------------------------------------------------------------
    void init_Port(void);         		// general system initialization
    void Init_Timer2(unsigned int counts);
    void Timer2_ISR(void);		//-- ISR for Timer 2
    
    void main(void)
    {
    	unsigned char blink_speed = NORMAL;
    	count = 0;
    
    	EA = 0;		//-- disable global interrupts
    				//-- It is a good idea to disable interrupts before all the initialisation
    				//-- is complete
    
    	//-- disable watchdog timer
    	WDTCN = 0xDE;
    	WDTCN = 0xAD;
    
    	init_Port();
    
    	if (blink_speed == NORMAL)
    		Init_Timer2(7203);	//-- Initialise Timer2 to generate interrupts every 350ms
    	else	//-- blink_speed is HIGH
    		Init_Timer2(53036);	//-- Initialise Timer2 to generate interrupts every 75ms
    
    	P5 = P5 & 0xFF;			//-- turn ON the four LEDs
    
    	EA = 1;		//-- enable global interrupts
    
    	while (1);	//-- go on forever (endless loop)
    
    }
    
    void init_Port(void)
    {
    	//----- Configure the XBRn Registers
    	XBR0 = 0x00;
    	XBR1 = 0x00;
    	XBR2 = 0x40;					  // Enable the crossbar, weak pullups enabled
    	// To disable weak pull-ups, XBR2 = 0xC0;
    
    	//------ Port configuration (0 = Open Drain Output, 1 = Push Pull Output)
    	P0MDOUT = 0x00;				  // Output configuration for P0 
    	P1MDOUT = 0x00;				  // Output configuration for P1
    	P2MDOUT = 0x00;				  // Output configuration for P2 
    	P3MDOUT = 0x00;				  // Output configuration for P3 
    
    	//-- Port 7-4 I/O Lines
    	P74OUT = 0x08;				  // Output configuration for P4-7
    								  // (P5[7:4] Push Pull) - 4 LEDs
    								  // (P5[3:0] Open Drain) - 4 Push-Button Switches (input)
    								  // (P4 Open Drain) - 8 DIP Switches (input)
    
    	//-- Write a logic 1 to those pins which are to be used for input
    	P5 |= 0x0F;
    	P4 = 0xFF;
    }
    
    //-- Configure Timer2 to auto-reload and generate an interrupt at interval
    //-- specified by <counts> using SYSCLK/12 as its time base.
    void Init_Timer2 (unsigned int counts)
    {
    	CKCON = 0x00;		//-- Define clock (T2M). Timer 2 uses system clock DIV BY 12
    //	CKCON |= 0x20;		//-- if you want to use system clock
    	T2CON = 0x00;		//-- T2CON.1 = 0 -->T2 set for Timer function (C/T2) i.e.
    						//--				incremented by clock defined by T2M
    						//-- T2CON.0 = 0 -->Allow Auto-reload on Timer2 overflow (CP/RL2)
    						//-- T2CON.3 = 0 -->High-to-Low transitions on T2EX ignored (EXEN2)
    						//-- T2CON.2 = 0 -->Disable Timer2
    
    	RCAP2 = counts;	//-- Init reload values in the Capture registers
    	T2 = 0xFFFF;		//-- count register set to reload immediately when the first clock occurs
    	IE |= 0x20;			//-- IE.5, Enable Timer 2 interrupts (ET2)
    	T2CON |= 0x04;		//-- start Timer2 by setting TR2 (T2CON.2) to 1
    }
    
    //-- Interrupt Service Routine
    //-- This routine changes the state of the LEDs whenever Timer2 overflows.
    void Timer2_ISR (void) interrupt 5
    {
    	T2CON &= ~(0x80);		//-- clear TF2. This is not automatically cleared, must be
    							//-- done by software upon overflow.
    
    	P5 = P5 ^ 0xF0;			//-- toggle the four LEDs using EX-OR with '1'
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The code you posted says:
    Code:
    if (blink_speed == NORMAL)
    		Init_Timer2(7203);	//-- Initialise Timer2 to generate interrupts every 350ms
    	else	//-- blink_speed is HIGH
    		Init_Timer2(53036);	//-- Initialise Timer2 to generate interrupts every 75ms
    That could lead to something interesting. (ETA: Granted I have no idea how 7203 and 53036 relate to 350 and 75, but hopefully you do.)
    Last edited by tabstop; 07-19-2011 at 10:06 AM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Wow. In 9 minutes you went from knowing nothing to coming up with that "fully working" program? I think you're too smart for all of us and you don't need our help. Wait...

    That code has nothing to do with your assignment. It blinks LEDs every 75 or 350 milliseconds. Nothing to do with precise .25 second delay. Actually, it looks like your code might have come from section 8.11 of this e-book: Embedded Programming or maybe from here: http://www.people.vcu.edu/~jhtucker/...ghter Card.pdf.
    Last edited by anduril462; 07-19-2011 at 10:36 AM. Reason: fixed borked link

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-04-2011, 03:20 PM
  2. 8051 tacho
    By CoCo in forum C Programming
    Replies: 7
    Last Post: 04-05-2011, 09:31 AM
  3. Micro-controller programming basics.
    By audiofish in forum C Programming
    Replies: 4
    Last Post: 03-26-2011, 09:38 AM
  4. c programming, MC68HC11 micro controller board simulator
    By fortune2k in forum C Programming
    Replies: 10
    Last Post: 03-11-2009, 07:04 AM
  5. Micro seconddelays
    By aspirantnew in forum C Programming
    Replies: 2
    Last Post: 03-01-2006, 12:20 PM