TB-1 Peripheral Board


TB-1 peripheral board example.

Toolkit:SDCC 8051 Development System

Location:/bipom/devtools/SDCC/examples/tb1/

Code Example


/*-----------DEFINES-----------------------------------------------------*/

#define RED             P1_3
#define YELLOW          P1_1
#define GREEN           P1_0
#define BUZZER          P1_2
#define SWITCHPORT      P3     

#define ADCCS           P3_7
#define ADCCLK          P1_6
#define ADCDIN          P1_4
#define ADCDOUT         P1		/* P1_4 */
 

void Buzz( unsigned frequency, unsigned duration ); 
void Clock();
unsigned char Convert( unsigned char channel);
       
main()
{
    unsigned i;

    /* Turn all lights off */
    RED = 1;
    YELLOW = 1;
    GREEN = 1;
    BUZZER = 1;

    /* Set the serial port to 9600 Baud */
    serinit(CBR_19200);

loop:

    printf("\n Checking LEDs and buzzer...");

    for (i=0; i<=5; i++) 
    {
        RED = 0; 
        Buzz(10+i,40);
        RED = 1;
        delay(50);
        
        YELLOW = 0; 
        Buzz(5+i,40);
        YELLOW = 1; 
        delay(50);
        
        GREEN = 0; 
        Buzz(3+i,40);
        GREEN = 1;
        delay(50);                
    }   
    
    /* Leave the buzzer off */
    BUZZER = 1;    
    
    printf("\nTesting Switches 1 and 2...\nYou may press any switch\n");
    printf("or press any key to exit switch test\n");
    
    while(!chkch()) 
    {        
        if( !(SWITCHPORT & 4))        
             printf("\nyou pressed switch 1\n");
             
        if( !(SWITCHPORT & 8))
             printf("\nyou pressed switch 2\n");              
             
		/* Delay a bit to debounce the switches */
		delay(250);             
   	}

   	printf( "\nTesting ADC..." );
   	
    while(!chkch()) 
    {        
    	for( i=0; i<4; i++ )
    	{
			printf( "\nCh0:%03d Ch1:%03d Ch2:%03d Ch3:%03d", 
					Convert(0),
					Convert(1),
					Convert(2),
					Convert(3) );
		}
	}							
	
	goto loop;
}


void Buzz(unsigned frequency, unsigned duration)
{
    unsigned i;
    
    for(i=0; i<duration; i++)
    {
		BUZZER = 1; 
		delay(frequency);
		BUZZER = 0; 
		delay(frequency);	            
	}
	
	BUZZER = 0; 
}

unsigned char Convert( unsigned char channel)
{
	unsigned char value;
	unsigned char i;
	
	/* Reset the A/D converter */	
	ADCCS = 1;
	ADCCLK = 0;
	ADCDIN = 1;
	
	/**** Start Conversion ****/
	
	ADCCS = 0;
	delay(1);
	
	/* Start bit */
	ADCDIN = 1;
	Clock();
	
	/* SGL/DIF */
	ADCDIN = 1;
	Clock();

	if( channel & 0x01 )
	{
		ADCDIN = 1;
		
	}
	else
	{
		ADCDIN = 0;
	}

		
	Clock();

	/* SELECT */
	
	if( channel & 0x02 )
	{
		ADCDIN = 1;
	}
	else
	{
		ADCDIN = 0;
	}
	
	Clock();
	
	/* 1 millisecond delay for the Multiplexer to settle */
	delay(1);
	Clock();
	
	value = 0;
	ADCDIN = 1;
	/* Clock out the 8-bit data from A/D converter */
	for( i=0; i<8; i++ )
	{
		if( ADCDOUT & 0x10 )
			value |= 1;		
					
		Clock();
		
		if( i < 7 )
			value = value << 1;		
	}
	
	for( i=0; i<8; i++ )
	{
		Clock();
	}
		
	return value;
}


void Clock()
{
	ADCCLK = 1;
	ADCCLK = 1;		/* A little delay */
	ADCCLK = 0;
}