int main(void)
{
	
	//Initialise the UART
	UART_Init(9600);
	UART_writeString("Welcome\r\n");
	
	//Initialise the I2C Interface at 200kHz
	I2C_init(200);
	
	//Initialise the RTC, and show whether oscillator started successfully
	if (RTC_Init(RTC_ADDRESS, 1, 1))
	{
		UART_writeString("Oscillator is Running\r\n");
	}
	else
	{
		UART_writeString("ERROR: Oscillator did NOT start\r\n");
	}
	
	
	//Read the Time from the RTC
	tempVar = RTC_GetTime(RTC_ADDRESS, &timeYear, &timeMonth, &timeDay, &timeWeekDay, &timeHr, &timeAmPm, &timeMin, &timeSec);	
	
	//If Year is one and month is one and day is one, assume time not set (these are the Power-On-Reset values).
	//Set the Time
	if ((timeYear == 1) && (timeMonth ==1) && (timeDay == 1))
	{
		tempVar = RTC_SetTime(RTC_ADDRESS, 15,12,31,5,23,1,59,15);
	}
	

	//Check if Oscillator Running after time was set
	tempVar = RTC_read(RTC_ADDRESS, MCP794_RTCWKDAY);	

	if (!(tempVar & (1<<MCP794_OSCRUN)))
	{
		UART_writeString("Oscillator NOT Running\r\n\r\n");
	}
	else
	{
		#if DEBUGLEVEL > 2
		UART_writeString("Oscillator is Running\r\n");
		#endif
	}
	
    while(1)
    {
		_delay_ms(5000);	//Only read the time every 5 seconds
		
		//Read the Time
		tempVar = RTC_GetTime(RTC_ADDRESS, &timeYear, &timeMonth, &timeDay, &timeWeekDay, &timeHr, &timeAmPm, &timeMin, &timeSec);	//Reset minutes to zero
		
		//Print the time over the UART
		UART_writeString("\r\nTimecheck: ");
		UART_printDecimal(timeDay,2);
		UART_writeString("/");
		UART_printDecimal(timeMonth,2);
		UART_writeString("/20");
		UART_printDecimal(timeYear,2);
		
		UART_writeString("   ");
		UART_printDecimal(timeHr,2);
		UART_writeString(":");
		UART_printDecimal(timeMin,2);
		UART_writeString(":");
		UART_printDecimal(timeSec,2);
		UART_writeString("\r\n");

		
    }
}
コード例 #2
0
ファイル: main.c プロジェクト: digibird1/PICkit2_WithMPLabX
int main()
{
    
  initShadowRegisters();  
    
  TRISC = 0; //RC0 as Output PIN
  //TRISC1 = 0;
  
  
  
  //Clear Analog Inputs and make pins digital pins
  ANSEL=0b00000001;//set RA0 as analog pin
  ANSELH=0;
  
  IRCF0 = 0b111;//set prescaler
  
  UART_Init();
  
  UART_writeString("Startup ... done\n");
  
  
  
  //ADC
    //Select ADC conversion clock Frc
    ADCON1bits.ADCS = 0b111;
     //Configure voltage reference using VDD
    ADCON0bits.VCFG = 0;
    //Select ADC input channel (RA0/AN0)
    ADCON0bits.CHS = 0;
    //Select result format right justified
    //right=1 is good when using all 10 bits the two bytes can be concatenated 
    //easily into an integer
    //left=0 is good when using the 8 most significant bits
    ADCON0bits.ADFM = 1;
    //Turn on ADC module
    ADCON0bits.ADON = 1;
  

        
  
  while(1)
  {
//---------------------------------------------    
    //Blink LED on RC0 and RC1
    SetBitReg(Reg_PORTC,0,HIGH);
    SetBitReg(Reg_PORTC,1,LOW);

    __delay_ms(100); // 100ms Delay

    SetBitReg(Reg_PORTC,0,LOW);
    SetBitReg(Reg_PORTC,1,HIGH);

    __delay_ms(100); // 100ms Delay
//---------------------------------------------        

    
//--------------------------------------------- 
    //UART communication
    while(UART_DataAvailable()>0){
        //char a=UART_ReadByte();
        //UART_writeByte(a);
        //UART_writeByte('\n');
        
        UART_writeNumber(UART_DataAvailable());
        UART_writeByte('\n');  
        
        UART_writeString(UART_ReadString());
        UART_writeByte('\n');
    }
    
    if(UART_DataAvailable()<0){
        UART_writeString("Data Lost Reset UART\n");
        UART_Reset();
    }
    
    //Print a Register
 // UART_writeBitPattern(ANSELH);
 // UART_writeByte('\n');
//---------------------------------------------        

    
//---------------------------------------------    
        
    //Read ADC
    Get_Inputs();
    unsigned int ADC_Value=0;
    
    ADC_Value=ADRESH<<8 | ADRESL;
 
    UART_writeNumber(ADC_Value);
    UART_writeByte('\n');
    
    if(checkBit(ShadowPortC,1)>0)
       SetBitReg(Reg_PORTC,2,HIGH); 
    

 //---------------------------------------------    
  }
  return 0;
  
 
}