Skip to content

Instantly share code, notes, and snippets.

@timtheguy
Last active March 29, 2017 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timtheguy/51e8754650eb764e6a0631cc2915bccb to your computer and use it in GitHub Desktop.
Save timtheguy/51e8754650eb764e6a0631cc2915bccb to your computer and use it in GitHub Desktop.
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include "delay.h"
//function prototypes
void update_seven_segment(void);
void setClk(void);
//initialize variables as unsigned chars (or unsigned bytes)
unsigned char mill = 0;
unsigned char tenths = 0;
unsigned char secs = 0;
unsigned char tenSecs = 0;
unsigned char minutes = 0;
unsigned char select = 0;
unsigned char disp_data[4];
unsigned char disptn[4];
unsigned char lastbuttons = 0;
unsigned char repeat = 0;
unsigned char runflag = 0; //only update clock if set
//array for seven segment pattern
const unsigned char segm_ptrn [38] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,
0x3d,0x76,0x74,0x1e,0x38,0x54,0x63,0x5c,
0x73,0x50,0x78,0x3e,0x1c,0x6e,0x08,0x40,
0x00,0x01,0x48,0x41,0x09,0x49
};
void main(void) {
//initialize Dragon12 board ports
setClk();
PTP = 0xFF; //turn off 7-segment display
DDRB = 0xFF; //portb = output
DDRP = 0xFF; //portp = output
DDRJ = 0xFF; //make port J an output port
PTJ = 0xFF; //make PJ1 high to disable LEDs
DDRH = 0x00; //initialize buttons
EnableInterrupts;
while(1) {
/**** update clock using your own code here ****/
delayby50us(4);
mill++;
if(mill==100) {
tenths++;
mill=0;
if(tenths==10){
secs++;
tenths=0;
if(tenSecs==6){
minutes++;
tenSecs=0;
secs=0;
}else if(secs%10 == 0){
tenSecs++;
secs=0;
}
}
}
disp_data[0]= minutes;
disp_data[1]= tenSecs;
disp_data[2]= secs;
disp_data[3]= tenths;
/**** update the numbers on seven segment ****/
update_seven_segment(); //updates what is shown on the seven segment
}
}
void update_seven_segment(void) {
int i = 0;
//get segment pattern for each number
for(i=0;i<4;i++)
disptn[i] = segm_ptrn[disp_data[i]];
//multiplexing display one digit at a time
//digit0:
PORTB = disptn[3];
PTP &= ~0x08;
PTP |= 0x07;
delayby50us(4);
//digit1:
PORTB = disptn[2];
PTP &= ~0x04;
PTP |= 0x0B;
delayby50us(4);
//digit2:
PORTB = disptn[1];
PTP &= ~0x02;
PTP |= 0x0D;
delayby50us(4);
//digit3:
PORTB = disptn[0];
PTP &= ~0x01;
PTP |= 0x0E;
delayby50us(4);
}
//this function must be called once to set up clock with correct speed
void setClk(void){
REFDV = 0x00;
SYNR = 0x02;
PLLCTL=0x60;
while(CRGFLG&0x08 == 0x00){ //busy wait
}
CLKSEL|=0x80;
}
@timtheguy
Copy link
Author

timtheguy commented Mar 29, 2017

#include <hidef.h>      	/* common defines and macros */
#include "derivative.h"		/* derivative-specific definitions */
#include "delay.h"

//function prototypes
void update_seven_segment(void);
void setClk(void);

//initialize variables as unsigned chars (or unsigned bytes)
unsigned char mill = 0;
unsigned char tenths = 0;
unsigned char secs = 0;
unsigned char tenSecs = 0;
unsigned char minutes = 0;
unsigned char select = 0;
unsigned char disp_data[4];
unsigned char disptn[4];

unsigned int counts = 0;

unsigned char lastbuttons = 0;
unsigned char repeat = 0;
unsigned char runflag = 0;     //only update clock if set

//array for seven segment pattern
const unsigned char segm_ptrn [38] = {
  0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
  0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,
  0x3d,0x76,0x74,0x1e,0x38,0x54,0x63,0x5c,
  0x73,0x50,0x78,0x3e,0x1c,0x6e,0x08,0x40,
  0x00,0x01,0x48,0x41,0x09,0x49 
};  


void main(void) {
	//initialize Dragon12 board ports
	setClk();
	PTP  = 0xFF;  //turn off 7-segment display
	DDRB = 0xFF;  //portb = output
	DDRP = 0xFF;  //portp = output
	DDRJ = 0xFF;  //make port J an output port
	PTJ  = 0xFF;  //make PJ1 high to disable LEDs
	DDRH = 0x00;  //initialize buttons
  
  
 
	EnableInterrupts;
	
	
	while(1) {   
		/**** update clock using your own code here ****/
		delayby50us(4);
		mill++;
		
		disp_data[0] = 10;
		disp_data[1] = 11;
		disp_data[2] = 12;
		disp_data[3] = 13;
		
		if(mill==100) {
		  tenths++;
		  mill=0;
		  if(tenths==10){
		    secs++;
		    tenths=0;
			if(tenSecs==5 && secs==9){
			  tenSecs=0;
			  secs=0;
			  minutes++;	  
			}else if(secs%5 == 0){
			  if(counts == 0){
				disp_data[0] = 14;
				disp_data[1] = 15;
				disp_data[2] = 16;
				disp_data[3] = 17; 
				counts++;
			  }else if(counts == 1){
				disp_data[0] = 32;
				disp_data[1] = 32;
				disp_data[2] = 32;
				disp_data[3] = 32;   
			  }
			  
			  tenSecs++;
			  secs=0;	
			}
		  }
		}
		
		
		
		
		
		//disp_data[0]= minutes;
		//disp_data[1]= tenSecs;
		//disp_data[2]= secs;
		//disp_data[3]= tenths;


		/**** update the numbers on seven segment ****/
		

		update_seven_segment(); //updates what is shown on the seven segment
   }
}

void update_seven_segment(void) {
	int i = 0;
  
	//get segment pattern for each number
	for(i=0;i<4;i++)
		disptn[i] = segm_ptrn[disp_data[i]];

	//multiplexing display one digit at a time 
	//digit0: 
	PORTB = disptn[3];
	PTP &= ~0x08;
	PTP |= 0x07; 
	delayby50us(4);

	//digit1: 
	PORTB = disptn[2];
	PTP &= ~0x04;
	PTP |= 0x0B;
	delayby50us(4);

	//digit2: 
	PORTB = disptn[1];
	PTP &= ~0x02;
	PTP |= 0x0D;
	delayby50us(4);

	//digit3: 
	PORTB = disptn[0];
	PTP &= ~0x01;
	PTP |= 0x0E;
	delayby50us(4);
}

//this function must be called once to set up clock with correct speed
void setClk(void){
	REFDV = 0x00;
	SYNR = 0x02;
	PLLCTL=0x60;
	while(CRGFLG&0x08 == 0x00){  //busy wait
	}

	CLKSEL|=0x80;
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment