» »

Programiranje v c-ju za 8051

Programiranje v c-ju za 8051

Dranon ::

Pozdravljen!
Zanima me ce kdo obvlada programiranje v c-ju za 8051(Keil uVision 3)? Sam sem totalni zacetnik kar se tice programiranja in bi potreboval par nasvetov. Rad bi uporabil 8051 kot kontroler za prehod za pesce. Port1 je povezan z lucmi na semaforju in pa z bleeperjem, medtem ko je Port2 input port za tipko na katero pesec pritisne ce zeli cez cesto. Tezava je v tem da neznam sprogramirati timerja ki bi mi dal razlicne delay-e. Potrebujem tri razlicno dolge delay-e:0.5s - za utripanje rumene luci; 3s za prehod iz rumene v rdeco; 15s za zeleno luc za pesca.
Trenutno je v programu samo en delay ki se pa bazira preprosto na tem da proc steje do 33000. Sam program se uspesno skompajlira, zagnal sem ga tudi v Keil simulatorju vendar je tezko reci ce dela OK ker gre vse skupaj prehitro. Ne me krizat zaradi uporabe "goto", zadnjic ko sem programiral sem uporabljal BASIC na C64, star sem bil 5 let. Bolj mi gre za to da program usposobim in se kaj naucim.
Vsaka pripomba oz. pomoc je vec kot dobrodosla.
Pa se koda:
<p><i>
#include <reg664.h>    //Register map for 8051.

sbit red_traf=P1^0;   //Red traffic light is connected to bit 0 of P1.
sbit amber_traf=P1^1; //Amber traffic light is connected to bit 1 of P1.
sbit green_traf=P1^2; //Green traffic light is connected to bit 2 of P1.
sbit red_ped=P1^3;    //Red pedestrian light is connected to bit 3 of P1.
sbit green_ped=P1^4;  //Green pedestrian light is connected to bit 4 of P1.
sbit bleeper=P1^5;    //Bleeper is connected to bit 5 of P1.
sbit button=P2^0;     //Pedestrian button is connected to bit 0 of P2.
unsigned char count;

void delay_vshort()     //This defines a very short delay.
  {
    unsigned int x;
    for (x=0;x<33000;x++);
  }

void delay_short() //This defines a short delay.
  {
    unsigned int x;
    for (x=0;x<33000;x++);
  }

void delay_long()          //This defines a long delay.
  {
    unsigned int x;
    for (x=0;x<33000;x++);
  }


main()
  {
    P1 = 0x00;                      //Reset all bits of P1.
    P2 = 0x00;                      //Reset all bits of P2.
    s0:
    amber_traf=0;		            //Reset amber traffic light.
    green_ped=0;		            //Reset green pedestrian light.
    green_traf=1;                   //Green traffic light is on.
    red_ped=1;                      //Red pedstrian light is on.
    if (button==1) goto s1;         //Check if button was pressed.
    else goto s0;

    s1:
    green_traf=0;                   //Switch off green traffic light.
    amber_traf=1;                   //Switch on amber traffic light.
    delay_short();                  //Leave amber traffic light on for a short time.

    s2:
    green_traf=0;                   //Switch off green traffic light.
    red_traf=1;                     //Swich on red traffic light.
    red_ped=0;                      //Switch off red pedestrian light.
    green_ped=1;                    //Switch on green pedestrian light.
    bleeper=1;                      //Switch on the bleeper.
    delay_long();                   //Remain in the same state for a long time.

    s3:
    red_traf=0;                     //Switch off red traffic light.
    for (count=0;count<5;count++)   //This will make amber traffic and green pedestrian light flash five times.
    {
       amber_traf = 1;               //Switch amber traffic light on.
       green_ped = 1;                //Switch green pedestrian light on.
       delay_vshort();               //Wait for a very short time.
       amber_traf = 0;               //Switch both previous lights off.
       green_ped = 0;
       delay_vshort();
    }

    s4:
    bleeper=0;                      //Turn off the bleeper.
    green_ped=0;                    //Turn off green pedestrian light.
    amber_traf=1;                   //Turn on amber traffic light.
    red_ped=1;                      //Turn on red pedestrian light.
    delay_short();                  //Wait for a short time.
    goto s0;                        //Return to the beginning.
  }
</i></p>

icek ::

Stavki, ki se v main zanki ponavljajo se napisejo v neskoncno zanko:
while(1)
{
stavki;
}

ali
for(;;)
{
stavki;
}

Ti trije delayi so vsi enaki. Ce so ti prekratki, povecaj konstanto (33000).
Rider of Theli
www.air-tv.net

Dranon ::

Hvala za reply.
Se zavedam do so delay-i vsi enaki, saj to je eden izmed problemov. Resitev z povecanjem konstante je zelo neelegantna, poleg tega
pa je(kolikor jaz vem) 8051 limitiran glede celih stevil ki jih lahko prebavi.

Tutankhamun ::

pa tko nared pa boš meu večji delay...
for (x = 0; x < 33000; ++x)
   for (y = 0; y < 33000; ++y)

Če je pa ta compiler preveč pametn pa zna optimizirat kodo, ti bo pa to for zanko k nč ne dela prevedu direkt v x = 33000 :P.

Pa vrjetn bi blo bol če bi naredu sam eno delay funkcijo, pol jo pa kličeš z različnimi delay-i.

void delay_vshort(int delay)
{
    unsigned int x;
    for (x = 0; x < delay; ++x);
}


pri s1 pa s2 kr postavlaš 2x zeleno na 0

U bed bug maš v kodi.
Pri s2 ne izklopš oranžne lučke, mau poprav :) sej je vrjetn še več napak, tko na hitrco sm najdu to.
AMD Phenom QUAD 9950 Black Edition, 8GB

Zgodovina sprememb…

Tutankhamun ::

Pa ko mi je lih dougcajt :P

#include <reg664.h>    //Register map for 8051.

#define ON     	1
#define OFF    	0
#define TOGGLE 	1

#define SHORT_DELAY   	10000
#define LONG_DELAY   	60000

sbit red_traf = P1^0;   //Red traffic light is connected to bit 0 of P1.
sbit amber_traf = P1^1; //Amber traffic light is connected to bit 1 of P1.
sbit green_traf = P1^2; //Green traffic light is connected to bit 2 of P1.
sbit red_ped = P1^3;    //Red pedestrian light is connected to bit 3 of P1.
sbit green_ped = P1^4;  //Green pedestrian light is connected to bit 4 of P1.
sbit bleeper = P1^5;    //Bleeper is connected to bit 5 of P1.
sbit button = P2^0;     //Pedestrian button is connected to bit 0 of P2.


void delay (int delay)
{
    unsigned int x;
    for (x = 0; x <= delay; ++x) {}
}

main()
{
	while (1) // infinite loop
	{
	    // turn all lights off
	    P1 = OFF;
	    P2 = OFF;

	    /* Go traffic */
	    // traffic: 	G = ON,  A = OFF, R = OFF
	    // pedestrians: G = OFF,	          R = ON
	    s0:
	    green_traf = ON;
	    red_ped    = ON;
		
	    while (button) {}		// wait for button

	    // traffic:     G = OFF, A = ON,  R = OFF
	    // pedestrians: G = OFF,  	      R = ON  - no change
	    s1:
	    green_traf = OFF;
	    amber_traf = ON;

	    delay (SHORT_DELAY);  // short delay

	    /* Go pedestrians */
	    // traffic: 	G = OFF, A = OFF, R = ON
	    // pedestrians: G = ON,               R = OFF
	    s2:
	    red_traf  = ON;
	    red_ped   = OFF;
	    green_ped = ON;

	    bleeper   = ON;	// turn bleeper on

	    delay (LONG_DELAY);	// long delay

	    /* Red trafic light is OFF and pedestrians still have green light. markomk U R fired.
			bwahaha, die fucking pedestrians !!!	
	    */
	    // traffic:     G = OFF, A = ON, R = OFF
	    // pedestrians: G = ON,          R = OFF
	    s3:
	    red_traf   = OFF;
	    amber_traf = ON; 

	    // amber traffic and green pedestrian light should flash five times.
	    unsigned char count;
	    for (count = 0; count < 5; ++count)   
	    {
		delay (SHORT_DELAY);	// short delay

		//toggle lights
		amber_traf ^= TOGGLE;              
		green_ped  ^= TOGGLE;
	    }
		
	    // traffic:     G = OFF, A = ON, R = OFF
	    // pedestrians: G = OFF,         R = ON
	    s4:
	    bleeper   = OFF;	// turn bleeper off

	    green_ped = OFF;
	    red_ped   = ON;

	    delay (SHORT_DELAY);	// short delay

	} // infinite loop
} // main
AMD Phenom QUAD 9950 Black Edition, 8GB

Zgodovina sprememb…

Dranon ::

Najlepsa ti hvala Tutankhamun! Si predstavljam kaj si si mislil, ko si videl "goto". BTW tisto v s2 ni bug je feature!
Sicer mi je pa koncno uspelo ugotoviti kako uporabiti interne timerje 8051-ke za delay-e.
Tule je koda:
void delay(int del)		//Delay procedure.
	{
	int c=0;		
	TMOD=0x01;		//CT1 not used, CT0 as a timer, in mode 01.
	TR0=0;
	TF0=0;
		do	
		{	
		TH0=0;		  	//Load CT0 with zero to get
		TL0=0;			//the longest possible time.
		TR0=1;			//Start timer 
		while(!TF0);	     //While Timer Flag is low, do nothing i.e.wait till it goes high. 
		TR0=0;			//Stop timer
		TF0=0;			//Make timer flag low again.
		c++;			
		}
	while (c<del);		
	}

Verjetno koda ni ravno optimalna, definitivno je neumno pokomentirana, dela pa le.
V programu potem samo poklicem npr delay(5), kar mi da ca 0.5s delay.
Pocasi le napredujem. Imam pa se eno tezavo. Rad bi dodal se lock-out funkcijo za stikalo na katero pritisne pesec, tako da ce je od zadnje zelene luci za pesce minilo manj od dolocenega casa in ce pesec spet pritisne stikalo le to ne bo delovalo takoj ampak po preteku nekega definiranega casa. To bi se verjetno dalo resiti z uporabo counterja ki bi stel koliko ciklov je minilo od zadnjega pritiska na gumb in ce je ciklov manj od neke definirane vrednosti stikalo nebi delovalo dokler seveda counter ne presteje do konca. Sama koda je sicer zelo podobna tisti za timer vendar ne vem kako naj jo inkorporiram v program.
Morda kaksna ideja?

Tutankhamun ::

Hmmm....
Ok pa recimo da ni bug, pa oranžna lučka pač gori. A pa si vidu en drug bug, da pobijaš uboge pešce :) al je pa to tvoj namen? Pri s3 imajo pešci še zmeri zeleno luč, avtomobili pa že kr oranžno.
Če js dobr vem, se pešcem najprej pržge rdeča, pol mau počaka, pol se pa pržge oranžna za promet...
Lahk popravš ;) da ne bo preveč žrtev...

Takrt ko postavš zeleno luč za traffic, štartj counter do kam nj šteje.

Pol pa tm ko prtisneš, pogledaš do kok je counter prešteu. Če je preštel do konca greš lepo naprej, če pa ni pa še vržeš en delay da počako za tisto razliko.


Primer:

1. Štartj counter ki naj šteje do 1000
2. če je gumb pritisnjen skoči na korak 4, drugače pejt na korak 3
3. skoči na korak 2 in spet preveri gumb
4. ustavi counter
5. delay (1000 - vrednost_counterja)
6. izklopi counter
7. pol pa naprej prižigaš lučke

Žal nism delal z 8051 procesorčki, samo s PICI pa Motorolo, tko da ti težko pomagam. Katere registre moreš nastavlat in kako...
AMD Phenom QUAD 9950 Black Edition, 8GB

Zgodovina sprememb…



Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Počasen internet (Telemach)

Oddelek: Omrežja in internet
144814 (3116) crniangeo
»

PS3 : Control for HDMI

Oddelek: Konzole
403392 (2921) bobnarmato
»

hp probook 4730s problem

Oddelek: Pomoč in nasveti
101865 (1766) amigo_no1
»

začetek programiranja 89c2051

Oddelek: Elektrotehnika in elektronika
161341 (1024) samouk
»

zvok s tv kartice

Oddelek: Zvok in slika
6889 (860) Jan987

Več podobnih tem