Page MenuHomec4science

main.c
No OneTemporary

File Metadata

Created
Sun, Sep 1, 13:19
/*
* File: main.c
* Author: tuleu
*
* Created on May 17, 2012, 3:12 PM
*/
/******************************************************************************
* Software License Agreement
*
* Copyright © 2011 Microchip Technology Inc. All rights reserved.
* Microchip licenses to you the right to use, modify, copy and distribute
* Software only when embedded on a Microchip microcontroller or digital
* signal controller, which is integrated into your product or third party
* product (pursuant to the sublicense terms in the accompanying license
* agreement).
*
* You should refer to the license agreement accompanying this Software
* for additional information regarding your rights and obligations.
*
* SOFTWARE AND DOCUMENTATION ARE PROVIDED ìAS ISî WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY
* OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR
* PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR
* OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION,
* BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT
* DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL,
* INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA,
* COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY
* CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF),
* OR OTHER SIMILAR COSTS.
*
*****************************************************************************/
#if defined(__PIC24E__)
#include <p24Exxxx.h>
#elif defined (__PIC24F__)
#include <p24Fxxxx.h>
#elif defined(__PIC24H__)
#include <p24Hxxxx.h>
#elif defined(__dsPIC30F__)
#include <p30Fxxxx.h>
#elif defined (__dsPIC33E__)
#include <p33Exxxx.h>
#elif defined(__dsPIC33F__)
#include <p33Fxxxx.h>
#endif
#include <sbcp-uc/sbcp.h>
#include <sbcp-uc/buffers.h>
#include <sbcp-uc/packet.h>
#include "power_sbcp.h"
#include "speaker.h"
#include "buttons.h"
#include "pindefs.h"
#include "spi_led.h"
#include "timer.h"
#include "adc.h"
//Disable watch dog
_FWDT(WDTPOST_PS32768 & WDTPRE_PR128 & WINDIS_OFF & FWDTEN_OFF)
//pic started by external clock.
_FOSC(POSCMD_NONE & OSCIOFNC_ON & FCKSM_CSECMD)
//external clock
_FOSCSEL(FNOSC_FRC & IESO_OFF)
_FGS(GSS_OFF & GWRP_OFF); // Code Protect off, Write protect disabled
_FPOR(FPWRT_PWR1); // No PWM off delay, PWM controlled by port configuration at device reset
_FICD(JTAGEN_OFF & ICS_PGD1); // No JTAG, debugging on PGD1 pins
extern unsigned int systime;
void clock_init()
{
// Disable Watch Dog Timer
RCONbits.SWDTEN=0;
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD = 41; // M = 43
CLKDIVbits.PLLPOST=0; // N2 = 2
CLKDIVbits.PLLPRE=0; // N1 = 2
// Initiate Clock Switch to Internal FRC with PLL (NOSC = 0b001)
__builtin_write_OSCCONH(0x01);
__builtin_write_OSCCONL(0x01);
// Wait for Clock switch to occur
while (OSCCONbits.COSC != 0b001){};
// Wait for PLL to lock
while(OSCCONbits.LOCK != 1) {};
}
/** \brief Entrance point of the program
* The entrance point
*/
int main() {
unsigned int i;
clock_init();
pin_init();
timer_init();
init_spi_led();
init_speaker();
init_buttons();
if (SW1 != 0) { //powerbutton is not pressed
POWER_EN = 0; // switch off own power supply
set_RGB_led(LD1, 1, 0, 0);
// no other stuff to do anymore, wait until charge is drowned
while (1);
}
init_adc();//shut down if there is something wrong with the power
adc_update();//make sure that sbcp can ask good data immediately
//wait for initializations to take full effect
for (i = 0; i < 4160; i++) {
Nop();
}
sbcp_settings s;
gpio send_enable = gpio_create(GPIO_PORT_B,6,GPIO_OUTPUT);
//gpio receive_disable = gpio_create(GPIO_PORT_B,4,GPIO_OUTPUT);
sbcp_init_settings(&s,0xeb,0x01,0x0001,send_enable,send_enable);
s.init_register = &(init_my_registers);
s.init_instruction = &(init_my_instructions);
s.klass = 0xeb;
s.id = 1;
// mapping everything
map_pin_to_periph_output(7,RP_U1TX);
uart_map_rx_input(UART_1,5);
sbcp_init(&s);
unsigned long prev_time = systime + 42;
RGB_led_test();
beep_hello();
while(1){
check_buttons();
adc_update();
// show status powerboard power supply
if(POWER_EN){
set_RGB_led(LD1, 0, 1, 0);
}else{
set_RGB_led(LD1, 1, 0, 0);
}
// show status servo power supply
if(SERVO_VOLTAGE_EN){
set_RGB_led(LD2, 0, 1, 0);
}else{
set_RGB_led(LD2, 1, 0, 0);
}
// show status motor power supply
if(MOTOR_VOLTAGE_EN){
set_RGB_led(LD3, 0, 1, 0);
}else{
set_RGB_led(LD3, 1, 0, 0);
}
//if timer 1 has interrupted every 1ms
if(systime != prev_time){
prev_time = systime;
sbcp_mark_new_low_latency_data_available();
}
sbcp_process();
}
return (0);
}

Event Timeline