Skip to content

Commit

Permalink
v2.5 LEDs PWM Control added
Browse files Browse the repository at this point in the history
  • Loading branch information
sxxngchxxllxx committed Sep 21, 2016
1 parent 03c4e84 commit 05c329f
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 92 deletions.
2 changes: 2 additions & 0 deletions CONFIG/BootLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ void YourLowPriorityISRCode();
//Clear the interrupt flag
//Etc.

timer0_isr();

} //This return will be a "retfie", since this is in a #pragma interruptlow section

#elif defined(__C30__)
Expand Down
3 changes: 3 additions & 0 deletions CONFIG/Init.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ void InitializeDevice(void)
// Initilaize the user buffer
InitializeUser();

// 160921 SCLee
TIMR0_init();

// Initialize the timer for counting 2ms
TIMR1_init();
}
Expand Down
11 changes: 11 additions & 0 deletions DEFINE/GlobalTypeVars.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ BYTE fallingTargetArrival = 0;
// 150930 YJ
UINT powerCounter = 0;

// 160921 SCLee
UINT wg_ledCtrl = 0;
UINT r_ledCtrl = 0;
UINT g_ledCtrl = 0;
UINT b_ledCtrl = 0;
UINT wg_ledPwmDuty = 0;
UINT r_ledPwmDuty = 0;
UINT g_ledPwmDuty = 0;
UINT b_ledPwmDuty = 0;
UINT tmr0l_temp = 0;

RxBuffer rxBuffer;
TxBuffer txBuffer;

Expand Down
11 changes: 11 additions & 0 deletions DEFINE/GlobalTypeVars.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ extern BYTE fallingTargetArrival;
// 150930 YJ For PWM Power Test
extern UINT powerCounter;

// 160921 SCLee
extern UINT wg_ledCtrl;
extern UINT r_ledCtrl;
extern UINT g_ledCtrl;
extern UINT b_ledCtrl;
extern UINT wg_ledPwmDuty;
extern UINT r_ledPwmDuty;
extern UINT g_ledPwmDuty;
extern UINT b_ledPwmDuty;
extern UINT tmr0l_temp;

/** Protect buffer *********************************/
extern RxBuffer rxBuffer;
extern TxBuffer txBuffer;
Expand Down
7 changes: 6 additions & 1 deletion DEFINE/UserDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ typedef struct _RXBUFFER

BYTE compensation;

BYTE reserved_for_64byte[38];
BYTE led_wg_pwm;
BYTE led_r_pwm;
BYTE led_g_pwm;
BYTE led_b_pwm;

BYTE reserved_for_64byte[34];
} RxBuffer;

typedef struct _TXBUFFER
Expand Down
3 changes: 1 addition & 2 deletions HardwareProfile - PICDEM FSUSB.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@

#define InitLEDs() {TRISDbits.TRISD0=0;LED_WG_OFF();TRISDbits.TRISD1=0;LED_R_OFF();TRISDbits.TRISD2=0;LED_B_OFF();TRISDbits.TRISD3=0;LED_G_OFF();}


/** SWITCH *********************************************************/
#define InitSwitches() TRISBbits.TRISB4=1;
#define sw2 PORTBbits.RB4
Expand Down Expand Up @@ -241,7 +240,7 @@

#define Set_PWM_plus_Out() {LATCbits.LATC2=0;TRISC &= ~(0x04);}
#define Set_PWM_minus_Out() {LATDbits.LATD5=0;TRISD &= ~(0x20);}

#define InitPWMPort() {Set_PWM_plus_Out();\
Set_PWM_minus_Out();}

Expand Down
18 changes: 13 additions & 5 deletions PCR/PCRTask.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ BYTE fanFlag = 0;
* Function : void PCR_Task(void)
* This function is overall routine for microPCR
**********************************/


void PCR_Task(void)
{
// Check the cmd buffer, performing the specific task.
Expand All @@ -55,11 +57,17 @@ void PCR_Task(void)
// Sensing the adc values(for photodiode, chamber, heatsink)
Sensor_Task();

if( rxBuffer.ledControl ){
LED_WG = rxBuffer.led_wg;
LED_R = rxBuffer.led_r;
LED_G = rxBuffer.led_g;
LED_B = rxBuffer.led_b;
if( rxBuffer.ledControl )
{
// 160921 SCLee
wg_ledCtrl = (UINT)rxBuffer.led_wg;
r_ledCtrl = (UINT)rxBuffer.led_r;
g_ledCtrl = (UINT)rxBuffer.led_g;
b_ledCtrl = (UINT)rxBuffer.led_b;
wg_ledPwmDuty = (UINT)rxBuffer.led_wg_pwm;
r_ledPwmDuty = (UINT)rxBuffer.led_r_pwm;
g_ledPwmDuty = (UINT)rxBuffer.led_g_pwm;
b_ledPwmDuty = (UINT)rxBuffer.led_b_pwm;
}

// Setting the tx buffer by structed buffer.
Expand Down
136 changes: 135 additions & 1 deletion PCR/Timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@
#include "./DEFINE/UserDefs.h"
#include "./PCR/Timer.h"

/***************************************************
* Function: void TIMR0_init(void)
*
* OverView: This routine will use low interrupt service routine configuration.
*
* Note: None
***************************************************/
void TIMR0_init(void)
{
//Timer0
T0CONbits.TMR0ON = 0; // stop timer

T0CONbits.T08BIT = 1; // 1:8bit , 0:16bit
T0CONbits.T0CS = 0; // Internal instruction cycle clock (CLKO)
T0CONbits.PSA=0; // Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output.

// T0CON = 0xC5;
T0CON = 0xC4;

//8bit setting
TMR0L = 0x06;
tmr0l_temp = 0x06;

// Enable interrupts
INTCON2bits.TMR0IP = 0; //Low priority
INTCONbits.GIEH=1; //Enables all unmasked interrupts
INTCONbits.GIEL=1; //Enables all unmasked peripheral interrupts
INTCONbits.TMR0IE=1; //Enables the TMR0 overflow interrupt

T0CONbits.TMR0ON = 1; //start timer
}

/***************************************************
* Function: void TIMR1_init(void)
Expand Down Expand Up @@ -40,7 +71,110 @@ void TIMR1_init(void)
INTCONbits.GIEL=1;//Enables all unmasked peripheral interrupts
PIE1bits.TMR1IE =1;

T1CONbits.TMR1ON = 1; //start timer
T1CONbits.TMR1ON = 1; //start timer
}

/***************************************************
* Function: void timer0_isr(void)
*
* OverView: If low interrupt actived, called this function.
* Control LED
*
* Note: reference BootLoader.h
***************************************************/
void timer0_isr(void)
{
if(INTCONbits.TMR0IF)
{
INTCONbits.TMR0IF = 0;
TMR0L = tmr0l_temp;

if(!wg_ledCtrl)
{
if(wg_ledPwmDuty <= 0)
{
TMR0L = wg_ledPwmDuty;
LED_WG_OFF();
}

else if(TMR0L == wg_ledPwmDuty)
{
TMR0L = 255 - wg_ledPwmDuty;
LED_WG_ON();
}
else
{
TMR0L = wg_ledPwmDuty;
LED_WG_OFF();
}
}
else if(!r_ledCtrl)
{
if(r_ledPwmDuty <= 0)
{
TMR0L = r_ledPwmDuty;
LED_R_OFF();
}

else if(TMR0L == r_ledPwmDuty)
{
TMR0L = 255 - r_ledPwmDuty;
LED_R_ON();
}
else
{
TMR0L = r_ledPwmDuty;
LED_R_OFF();
}
}
else if(!g_ledCtrl)
{
if(g_ledPwmDuty <= 0)
{
TMR0L = g_ledPwmDuty;
LED_G_OFF();
}

else if(TMR0L == g_ledPwmDuty)
{
TMR0L = 255 - g_ledPwmDuty;
LED_G_ON();
}
else
{
TMR0L = g_ledPwmDuty;
LED_G_OFF();
}
}
else if(!b_ledCtrl)
{
if(b_ledPwmDuty <= 0)
{
TMR0L = b_ledPwmDuty;
LED_B_OFF();
}

else if(TMR0L == b_ledPwmDuty)
{
TMR0L = 255 - b_ledPwmDuty;
LED_B_ON();
}
else
{
TMR0L = b_ledPwmDuty;
LED_B_OFF();
}
}
else
{
TMR0L = tmr0l_temp;
LED_WG_OFF();
LED_R_OFF();
LED_G_OFF();
LED_B_OFF();
}
tmr0l_temp = TMR0L;
}
}

/***************************************************
Expand Down
Binary file removed README.docx
Binary file not shown.
89 changes: 8 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,84 +1,11 @@
# MicroPCR_Firmware
# Photodiode 와 temperature sensor 간섭 문제 #
# Micro PCR Firmware - LED PWM 제어 기능 추가 #
**2016-09-21 이승철**
##

**2016-02-02 김종대**
MicroPCR은 4개의 LED가 digital port를 사용하므로, 일반적인 PWM 제어를 할 수 없다.

**Experimenter: 이승철, 이득주**
따라서 타이머를 이용한 Software PWM을 구현해 4개의 LED를 PWM 제어를 한다.

1. 간섭문제의 원인으로 예상되는 것은 Photodiode input이 Vref=3.3V를 넘게 input으로 들어가서 이것으로 보임.

A. Analog input에 5V를 직접 연결하면 5.6도 올라간다

B. 3.3V 를 연결하면 온도의 변화가 없다

C. Temperature ADC 시작전에 한번 ADC를 하면 5V 입력일 때보다 0.7도 낮아진다.


2. 결론: Photodiode가 4096를 넘기 전까지는 문제가 없을 것으로 생각되며, 차후 3.3V를 넘지 못하게 하는 회로가 필요할 것으로 생각된다.

3. 추가로 발견된 SW flaw

**A. ADC conversion clock setting 오류**

HardwareProfile - PICDEM FSUSB.h를 보면 아래 2line으로 ADC conversion clock을 만드는 데 파란색 부분이 111이 되어야 한다.
이와 같은 오류로 해서 Fosc/64로 했으나 사실은 Fosc/32가 된다.
즉, Tad=1/(48M/32)=0.67us (Table 28-29: A/D conversion requirement에 어긋남)

#define SetADCConvClock(CLOCK) {ADCON2= (ADCON2&0b11111000) | (CLOCK & 0b00000011);}
#define ADC_ADCS_FREQD64 (0b00000110)

한편 sampling time 인 Tacq는 아래에 의하면 6*Tad 이므로 4us 이다
#define SetADCAcqTime(TIME) {ADCON2= (ADCON2&0b11000111) | (TIME&0b00111000);}
#define ADC_ACQT_06TAD (0b00011000)

**B. 수정**

#define SetADCConvClock(CLOCK) {ADCON2= (ADCON2&0b11111000) | (CLOCK & 0b00000111);}

Tad=1/(48M/64)=1.33 us
Tacq=8us > 2.35us (channel change후 requirement)
Tc = 6Tad+12Tad=18Tad=24us

10번 ADC하는 데 걸리는 시간 240us

**C. ADC 시간 실험 (README.docx 파일 참고)**

![ADC는 두번 실행되고 약 260us정도 됨](http://imgur.com/XbCMSUg)
ADC는 두번 실행되고 약 260us정도 됨

**D. 과거 실험 (README.docx 파일 참고)**

Tad=1/(48M/32)=0.67us 이고, ADC_ACQT_20TAD 일 때 10번 ADC loop 결과
Tc=32Tad=21.4us

10번 ADC 하는 데 걸리는 시간 214us
![](http://imgur.com/DwTRvIa)

**E. Main loop period가 3ms인 문제**

이번 측정에서 전체 주기가 3ms인 것은 아래 code문제임.
Counter가 ‘0’이 된 후에 1ms후에 1이 되고 다시 increment한다.
다시 1ms 후에 2가 되고 1ms후에 if condition으로 check되어 0으로 reset됨.

if( T2MS_Counter >= 2 )
{
T2MS_Flag = TRUE;
T2MS_Counter = 0;
T30MS_Counter++;
}
else
{
T2MS_Counter++;
}
2ms 마다 reset하려면 아래와 같이 수정해야 함.
T2MS_Counter++;
if( T2MS_Counter >= 2 )
{
T2MS_Flag = TRUE;
T2MS_Counter = 0;
T30MS_Counter++;
}
##
#주의#
본 Firmware는 최신 MicroPCR_MFC (v2.6) 또는 Labgenius_ver2 를 사용해야 합니다.
8 changes: 6 additions & 2 deletions microPCR.mcp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ file_027=.
file_028=.
file_029=PCR
file_030=PCR
file_031=.
file_031=PCR
file_032=.
[GENERATED_FILES]
file_000=no
file_001=no
Expand Down Expand Up @@ -89,6 +90,7 @@ file_028=no
file_029=no
file_030=no
file_031=no
file_032=no
[OTHER_FILES]
file_000=no
file_001=no
Expand Down Expand Up @@ -122,6 +124,7 @@ file_028=no
file_029=no
file_030=no
file_031=no
file_032=no
[FILE_INFO]
file_000=USB\usb_device.c
file_001=USB\usb_function_hid.c
Expand Down Expand Up @@ -154,7 +157,8 @@ file_027=HardwareProfile - PICDEM FSUSB.h
file_028=HardwareProfile.h
file_029=PCR\PCRTask.h
file_030=PCR\Temp_Table.h
file_031=rm18f4553 - HID Bootload.lkr
file_031=PCR\Timer.h
file_032=rm18f4553 - HID Bootload.lkr
[SUITE_INFO]
suite_guid={5B7D72DD-9861-47BD-9F60-2BE967BF8416}
suite_state=
Expand Down
Loading

0 comments on commit 05c329f

Please sign in to comment.