- #include "msp430x21x2.h"
- #define PWM_OUTPUT 0x03
- #define DUTY_CYCLE TA0CCR2
- #define MAX_LIMIT 300
- /*
- #define no_of_samples 1
- #define duty_cycle_limit 1000
- #define temp_sensor 0xA000
- #define panel_selection_cutoff 250
- */
- #define PV_VOLTAGE INCH_0
- #define PV_CURRENT INCH_1
- #define BATT_VOLTAGE INCH_2
- #define BATT_CURRENT INCH_3
- #define UPPER_CRG_LVL 0x0105 // 0x00FF // 0x00F5 //0x023A
- #define LOWER_CRG_LVL 0x00FA // 0x00F5// 0x00EA// 0x00DA
- #define UPPER_DIFF 2//0x10
- #define LOWER_DIFF 0//0x05
- #define UPPER_CUTOFF 0x00C8 // 0x00BF // 0x00B0
- #define LOWER_CUTOFF 0x00B8 // 0x00AE // 0x00A2
- #define SHORT_CKT_CURRENT 0x01C0 //for 750 mA //0x0085 for 550 mA
- #define MAX_CURRENT_LIMIT 0x0310
- #define POOR_BATTERY_VOLTAGE 0x00C5 // 0x00B8 // 0x00A8 //0x00AA for 6.75V
- #define RED_INDICATOR 0x04
- #define YELLOW_INDICATOR 0x02
- #define GREEN_INDICATOR 0x01
- #define LOAD_RELAY 0x08
- #define TRICLE_CKT 0//0x01
- #define TURN_OFF_CKT 4//0x10
- #define SHORT_CKT 5//0x20
- #define POOR_INDICATION 7//0x80
- unsigned int tricle_level = LOWER_CRG_LVL;
- unsigned int tricle_indicator_counter;
- unsigned char tricle_circuit = 0;
- unsigned char pannel_cutoff_voltage = 40;
- unsigned int load_switching_voltage = LOWER_CUTOFF;
- unsigned int previous_panel_voltage;
- unsigned int previous_panel_current;
- unsigned int recent_panel_voltage;
- unsigned int recent_panel_current;
- unsigned long recent_panel_power;
- unsigned long previous_panel_power;
- int panel_voltage_difference;
- int panel_current_difference;
- long panel_power_difference;
- double incremental_conductance;
- double instantanious_conductance;
- unsigned int battery_voltage;
- unsigned int battery_current;
- unsigned int temperature; //20 degC - 0x02E1, 32deg - 0x02EB, 50 deg - 0x02FA
- unsigned int adc_result;
- unsigned int sensor_voltage;
- unsigned char transmit_buffer[16] = {0xAA,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- unsigned char transmision_index = 0;
- unsigned char boolean_bits = 0x00;
- unsigned char discharge_ckt_off = 0;
- unsigned char short_ckt_active = 0;
- unsigned char discharge_ckt_on_counter = 0;
- unsigned char discharge_ckt_off_counter = 0;
- unsigned char short_ckt_on_counter = 0;
- unsigned char short_ckt_off_counter = 0;
- unsigned char tricle_ckt_on_counter = 0;
- unsigned char tricle_ckt_off_counter = 0;
- unsigned int poor_indication_counter = 0;
- unsigned char poor_indication_decision_counter = 0;
- unsigned int i;
- unsigned int j;
- #pragma vector = TIMER1_A0_VECTOR
- __interrupt void ISR_TIMER1_A0()
- {
- if(boolean_bits&(1<<TRICLE_CKT))
- {
- tricle_indicator_counter ++;
- }
- if(boolean_bits&(1<<POOR_INDICATION))
- {
- poor_indication_counter ++;
- }
- }
- #pragma vector=USCIAB0TX_VECTOR
- __interrupt void USCI0TX_ISR(void)
- {
- UCA0TXBUF = transmit_buffer[transmision_index];
- if(transmision_index >= 15)
- {
- transmision_index = 0;
- // UCSRB &= ~(1<<TXEN);
- }
- else transmision_index ++;
- }
- void initialize_TimerA0()
- {
- TA0CCR0 = MAX_LIMIT; // PWM Period
- TA0CCTL2 = OUTMOD_7; // CCR1 Reset/Set
- TA0CCR2 = 180; // CCR1 PWM duty cycle
- TA0CTL = TASSEL_2 + MC_1; // SMCLK, up mode
- }
- void initialize_TimerA1() // Timer setup
- {
- TA1CCR0 = 40000;//55556;
- TA1CCTL0 = 0x0010;
- TA1CTL = TASSEL_2 + MC_3; // SMCLK, up/down mode
- }
- void initialize_UART()
- {
- P3SEL |= 0x10; // P3.4,5 = USCI_A0 TXD/RXD
- P3DIR |= 0x10; // All P3.x outputs
- P3OUT = 0; // All P3.x reset
- UCA0CTL1 |= UCSSEL_2; // SMCLK
- UCA0BR0 = 0x83; //226; // 12MHz 9600
- UCA0BR1 = 0x06; //4; // 12MHz 9600
- // UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
- UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
- IE2 |= UCA0TXIE; // Enable USCI_A0 TX interrupt
- }
- void initialize_ADC10()
- {
- ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON;// + ADC10IE; // + ADC10SR + REF2_5V + REFOUT 1.5V Reference
- // ADC10CTL1 = (ADC10DIV_0 | ADC10SSEL_1);
- }
- unsigned int get_ADC_Data(unsigned int channel_no)
- {
- unsigned char a;
- adc_result = 0;
- // ADC10CTL1 |= channel_no;
- ADC10CTL0 |= ADC10ON;
- for(a=0;a<8;a++)
- {
- while(ADC10CTL1 & ADC10BUSY);
- ADC10CTL0 &= ~ENC;
- // ADC10CTL1 = channel_no + ADC10SSEL_1; // + ADC10DIV_4
- ADC10CTL1 = channel_no + ADC10DIV_7 + ADC10SSEL_3;
- switch(channel_no)
- {
- case INCH_0: ADC10AE0 = 0x01;
- break;
- case INCH_1: ADC10AE0 = 0x02;
- break;
- case INCH_2: ADC10AE0 = 0x04;
- break;
- case INCH_3: ADC10AE0 = 0x08;
- break;
- case INCH_4: ADC10AE0 = 0x10;
- break;
- case INCH_5: ADC10AE0 = 0x20;
- break;
- case INCH_6: ADC10AE0 = 0x40;
- break;
- case INCH_7: ADC10AE0 = 0x80;
- break;
- default : ADC10AE0 = 0x00;
- break;
- }
- ADC10CTL0 |= ENC + ADC10SC;
- while((ADC10CTL0 & ADC10IFG)==0);
- ADC10CTL0 &= ~ADC10IFG;
- adc_result += ADC10MEM;
- }
- ADC10CTL0 &= ~ADC10ON;
- adc_result = (adc_result >> 3);
- ADC10CTL1 &= ~channel_no;
- return adc_result;
- }
- void main (void)
- {
- // unsigned char observation_counter = 0;
- WDTCTL = WDTPW + WDTHOLD; // Stop WDT
- DCOCTL = 0x93; //CALDCO_16MHZ;
- BCSCTL1 = 0x8F; //CALBC1_16MHZ; // Set range for max frequency
- // DCOCTL = 0x81; //CALDCO_12MHZ;
- // BCSCTL1 = 0x8E; //CALBC1_12MHZ; // Set range for max frequency
- P1DIR = 0x80; // P1.7 PWM output
- P1SEL = 0x80;
- P2DIR = 0x00;
- P3DIR = (RED_INDICATOR | GREEN_INDICATOR | YELLOW_INDICATOR | LOAD_RELAY);
- P3OUT &= ~(RED_INDICATOR | GREEN_INDICATOR | YELLOW_INDICATOR | LOAD_RELAY);
- initialize_TimerA0();
- initialize_TimerA1();
- initialize_UART();
- initialize_ADC10();
- __bis_SR_register(GIE);
- P3OUT |= GREEN_INDICATOR;
- for(i=0;i<4000;i++)
- for(j=0;j<1000;j++);
- // P3OUT &= ~GREEN_INDICATOR;
- while(1)
- {
- for(i=0;i<800;i++)
- for(j=0;j<1000;j++);
- // sensor_voltage = get_ADC_Data(BATT_VOLTAGE);
- recent_panel_voltage = get_ADC_Data(PV_VOLTAGE);
- recent_panel_current = get_ADC_Data(PV_CURRENT);
- battery_voltage = get_ADC_Data(BATT_VOLTAGE);
- battery_current = get_ADC_Data(BATT_CURRENT);
- if(recent_panel_voltage > (battery_voltage + 5))
- {
- if(P3IN & (RED_INDICATOR|YELLOW_INDICATOR|LOAD_RELAY))
- {
- P3OUT &= ~(RED_INDICATOR | YELLOW_INDICATOR | LOAD_RELAY);
- }
- if(battery_voltage < LOWER_CRG_LVL) // 700 -- 18.5v , 600 -- 18v
- {
- if(recent_panel_voltage > 0x0130)
- {
- if((DUTY_CYCLE < (MAX_LIMIT-50)) && (recent_panel_current < MAX_CURRENT_LIMIT))
- {
- DUTY_CYCLE += 1;
- }
- else
- {
- if((DUTY_CYCLE > 0) && (recent_panel_current > MAX_CURRENT_LIMIT))
- {
- DUTY_CYCLE -= 1;
- }
- }
- }
- else
- {
- DUTY_CYCLE -= 1;
- }
- }
- else
- {
- if((battery_voltage > UPPER_CRG_LVL)&&(boolean_bits&(1<<TRICLE_CKT)))
- {
- DUTY_CYCLE -= 1;
- }
- }
- //...................................Temperature Compensation.......................................................
- /*
- temperature = get_ADC_Data(temp_sensor);
- if(temperature > 758) // If temp is greater than 45 degC
- {
- tricle_level = 276; //253;
- }
- else if(temperature > 750) // If temp is greater than 35 degC
- {
- tricle_level = 278; //255;
- }
- else if(temperature > 742) // If temp is greater than 25 degC
- {
- tricle_level = 280; //256;
- }
- else if(temperature > 735) // If temp is greater than 18 degC
- {
- tricle_level = 282; //258;
- }
- */
- //......................................... Tricle Charging ...........................................................
- if((battery_voltage >= (tricle_level + 0))&&(!(boolean_bits&(1<<TRICLE_CKT))))
- {
- tricle_ckt_off_counter = 0;
- tricle_ckt_on_counter++;
- if(tricle_ckt_on_counter>5)
- {
- boolean_bits |= (1<<TRICLE_CKT);
- tricle_ckt_on_counter = 0;
- }
- }
- else if((battery_voltage < (tricle_level - 5))&&(boolean_bits&(1<<TRICLE_CKT)))
- {
- tricle_ckt_on_counter = 0;
- tricle_ckt_off_counter++;
- if(tricle_ckt_off_counter>10)
- {
- boolean_bits &= ~(1<<TRICLE_CKT);
- tricle_ckt_off_counter = 0;
- }
- }
- else
- {
- tricle_ckt_on_counter = 0;
- tricle_ckt_off_counter = 0;
- }
- if(boolean_bits&(1<<TRICLE_CKT))
- {
- if(tricle_indicator_counter > 150)
- {
- P3OUT ^= GREEN_INDICATOR;
- tricle_indicator_counter = 0;
- }
- }
- else
- {
- if(!(P3IN&GREEN_INDICATOR))
- {
- P3OUT |= GREEN_INDICATOR;
- tricle_indicator_counter = 0;
- }
- }
- ///////////////////////////////////////////////// End of Charging Gircuit //////////////////////////////////////////////////////////////////////////////
- }
- else
- {
- if(P3IN&GREEN_INDICATOR)
- {
- P3OUT &= ~GREEN_INDICATOR;
- }
- if(DUTY_CYCLE > 180)
- {
- DUTY_CYCLE -= 1;
- }
- // .......................................... Discharge Circuit ................................................
- if(battery_voltage <= load_switching_voltage)
- {
- discharge_ckt_on_counter = 0;
- discharge_ckt_off_counter ++;
- if((discharge_ckt_off_counter > 15)&&(!(boolean_bits&(1<<TURN_OFF_CKT))))
- {
- boolean_bits |= (1<<TURN_OFF_CKT);
- discharge_ckt_off_counter = 0;
- load_switching_voltage = UPPER_CUTOFF;
- }
- }
- else
- {
- discharge_ckt_off_counter = 0;
- discharge_ckt_on_counter ++;
- if((discharge_ckt_on_counter > 10)&&(boolean_bits&(1<<TURN_OFF_CKT)))
- {
- boolean_bits &= ~(1<<TURN_OFF_CKT);
- discharge_ckt_on_counter = 0;
- load_switching_voltage = LOWER_CUTOFF;
- }
- }
- if(battery_current > SHORT_CKT_CURRENT)
- {
- short_ckt_off_counter = 0;
- short_ckt_on_counter ++;
- if(short_ckt_on_counter > 0)
- {
- boolean_bits |= (1<<SHORT_CKT);
- short_ckt_on_counter = 0;
- }
- }
- else
- {
- if((boolean_bits&(1<<SHORT_CKT)))
- {
- short_ckt_on_counter = 0;
- short_ckt_off_counter ++;
- if(short_ckt_off_counter > 60)
- {
- boolean_bits &= ~(1<<SHORT_CKT);
- short_ckt_off_counter = 0;
- }
- }
- }
- if((!(boolean_bits&(1<<TURN_OFF_CKT))) && (!(boolean_bits&(1<<SHORT_CKT))) && (!(P3IN&LOAD_RELAY)))
- {
- P3OUT |= LOAD_RELAY;
- P3OUT &= ~RED_INDICATOR;
- }
- if(((boolean_bits&(1<<TURN_OFF_CKT)) || (boolean_bits&(1<<SHORT_CKT))) && (P3IN&LOAD_RELAY))
- {
- P3OUT &= ~LOAD_RELAY;
- P3OUT |= RED_INDICATOR;
- }
- //............................................ Poor Indication ....................................................
- if((battery_voltage <= POOR_BATTERY_VOLTAGE) && (!(boolean_bits&(1<<TURN_OFF_CKT))))
- {
- poor_indication_decision_counter ++;
- if((poor_indication_decision_counter > 15) && (!(boolean_bits&(1<<POOR_INDICATION))))
- {
- boolean_bits |= (1<<POOR_INDICATION);
- poor_indication_decision_counter = 0;
- }
- }
- else
- {
- poor_indication_decision_counter = 0;
- if(((battery_voltage > (POOR_BATTERY_VOLTAGE + 10)) || (boolean_bits&(1<<TURN_OFF_CKT))) && (boolean_bits&(1<<POOR_INDICATION)))
- {
- boolean_bits &= ~(1<<POOR_INDICATION);
- }
- }
- if(boolean_bits&(1<<POOR_INDICATION))
- {
- if((poor_indication_counter > 400) && (!(P3IN & YELLOW_INDICATOR)))
- {
- P3OUT |= YELLOW_INDICATOR;
- poor_indication_counter = 0;
- }
- if((poor_indication_counter > 100) && (P3IN & YELLOW_INDICATOR))
- {
- P3OUT &= ~YELLOW_INDICATOR;
- poor_indication_counter = 0;
- }
- }
- else
- {
- P3OUT &= ~YELLOW_INDICATOR;
- }
- }
- //.....................................................................................................................
- previous_panel_voltage = recent_panel_voltage;
- previous_panel_current = recent_panel_current;
- previous_panel_power = recent_panel_power;
- transmit_buffer[2] = recent_panel_voltage;
- transmit_buffer[1] = (recent_panel_voltage >> 8);
- transmit_buffer[4] = recent_panel_current;
- transmit_buffer[3] = (recent_panel_current >> 8);
- transmit_buffer[6] = battery_voltage;
- transmit_buffer[5] = (battery_voltage >> 8);
- transmit_buffer[8] = battery_current;
- transmit_buffer[7] = (battery_current >> 8);
- transmit_buffer[12] = DUTY_CYCLE;
- transmit_buffer[11] = (DUTY_CYCLE >> 8);
- transmit_buffer[14] = short_ckt_off_counter;
- transmit_buffer[15] = boolean_bits;
- }
- }
1. What is a Stub function? Ans : A function without any definition which presents no error when called. 2. Why there are two ld scripts generated by STM32Cube IDE? Ans: The CubeIDE always presents two different LDscripts on for generating the executable for Debugging which goes to RAM another for normal code upload that goes to flash memory. 3. What is Supervisory Mode/ privileged mode ? Ans : When you are using a Kernel then there are two modes user mode and privileged mode. In User mode the applications cant have system calls. 4. What is "make -j6" ? Ans : Make has an argument for number of jobs, so when you add -j6 it will create 6 different compiler instances, so that the systems could use multicore to the fullest. Linux people generally do add "-j $(nproc)" where nproc is a command line which returns number of processors attached. 5. What is weak attribute ? Ans : Weak attribute used to denote weak symbols which helps linkers to choose one function out of mul...
Comments
Post a Comment