/Designs/STOPWATCH02A/SW/main.c
1,10 → 1,36
#include "main.h"
 
unsigned int position;
unsigned int32 section[4];
unsigned int32 time; // pocitadlo preteceni casovace
unsigned int state;
 
#define RUNNING 1
#define STOPPED 0
 
#int_RB
void RB_isr(void)
{
section[position]= get_timer0();
time=0;
set_timer0(0);
if(position < 3) position++;
else {
disable_interrupts(INT_RB);
state = STOPPED;
}
time=0;
set_timer0(0);
 
}
 
#int_RTCC
void RTCC_isr(void) // preruseni od pretekleho casovace
{
time++;
}
 
#define LCD_ENABLE_PIN PIN_E0
#define LCD_RS_PIN PIN_E1
#define LCD_RW_PIN PIN_E2
15,52 → 41,95
 
#include <lcd.c>
 
//#define USB_CON_SENSE_PIN PIN_B2
#define BUTTON1 PIN_D0
#define BUTTON2 PIN_D1
#define BUTTON3 PIN_D2
#define BUTTON4 PIN_D3
 
#define BEEPER PIN_C0
 
unsigned int32 run;
 
// Includes all USB code and interrupts, as well as the CDC API
#include <usb_cdc.h>
 
#rom int 0xf00000={1,2,3,4}
void beep(int16 period)
{
while(period){
output_toggle(BEEPER);
delay_us(800);
period--;
}
}
 
void test_mode()
{
lcd_gotoxy(1,1);
printf(lcd_putc,"TEST MODE");
}
 
void main() {
int i, j, address, value;
 
setup_adc_ports(NO_ANALOGS|VSS_VDD);
port_b_pullups(TRUE);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
// enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
 
lcd_init();
usb_init();
lcd_init();
 
lcd_gotoxy(1,1);
printf(lcd_putc, "\fReady...\n");
printf(lcd_putc,"STOPWATCH01A");
lcd_gotoxy(1,2);
printf(lcd_putc," Kaklik 2010 ");
delay_ms(1000);
lcd_gotoxy(1,1);
printf(lcd_putc,"\n Waiting for PC... ");
 
while(!usb_cdc_connected());
lcd_gotoxy(1,1);
printf(lcd_putc,"PC connected...");
beep(10);
while(!usb_cdc_connected());
if(!input(BUTTON1)) test_mode();
run=0;
section[0]=0;
section[1]=0;
section[2]=0;
section[3]=0;
while (TRUE)
{
run++;
lcd_gotoxy(3,1);
printf(lcd_putc,"START");
 
do {
printf(usb_cdc_putc, "\r\n\nEEPROM:\r\n"); // Display contents of the first 64
for(i=0; i<=3; ++i) { // bytes of the data EEPROM in hex
for(j=0; j<=15; ++j) {
printf(usb_cdc_putc, "%2x ", read_eeprom( i*16+j ) );
}
printf(usb_cdc_putc, "\n\r");
}
printf(usb_cdc_putc, "\r\nLocation to change: ");
address = gethex_usb();
printf(usb_cdc_putc, "\r\nNew value: ");
value = gethex_usb();
while(input(BUTTON2));
time = 0;
set_timer0(0);
position = 0;
state = RUNNING;
clear_interrupt(int_timer0);
clear_interrupt(int_RB);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_RB);
lcd_gotoxy(6,1);
printf(lcd_putc,"STOP");
while(state==RUNNING);
 
write_eeprom( address, value );
 
} while (TRUE);
printf(usb_cdc_putc, "\n\r %ld %ld %ld %ld %ld", run, section[0], section[1], section[2], section[3]);
}
}