dighedip@gmail.com
Newbie level 5
- Joined
- Jun 25, 2013
- Messages
- 8
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 135
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 #include <regx55.H> //*****DisplayFunctions***************************************************************** void display_data(unsigned int data_N_display); void delay(unsigned int itime); unsigned int c=0,x=0; //initialize key counter and key flag unsigned char array[]= {0xC0,0x79,0x24,0x30,0x19,0x12,0x02,0xF8,0x00,0x10}; unsigned char dig_ctl[] = {0x01,0x02,0x04,0x08} ; //unsigned char dig_ctl[] = {0x08,0x04,0x02,0x01} ; unsigned char digit[4]; unsigned char dig_num; //************************************************** void timer0(void) interrupt 1 // timer o only for display;; { //unsigned char index; //P0 = 0xFF; P2 = 0x00; //for (index=0;index<4;index++) //{ //P2 = dig_ctl[index]; //P0 = digit[index]; //} P0 = digit[dig_num]; P2 = dig_ctl[dig_num]; dig_num++; if(dig_num >=4) dig_num = 0; } //************************************************** void display_data(unsigned int data_N_display) { unsigned char temp_data; unsigned char index; unsigned long quotient; unsigned char temp_digit[5]; for(index=0;index<4;index++) { quotient = data_N_display/10; temp_data = data_N_display-quotient * 10; temp_digit[index] = temp_data; data_N_display = quotient; } for(index = 0;index<4;index++) { digit[index] = array[temp_digit[index]]; } //for (index=0;index<4;index++) //{ //P2 = dig_ctl[index]; //P0 = digit[index]; //} } //================================================== unsigned char ch; long COUNT, tlong; //************************************************** //************** CS5460******************** //sbit CS5460AResetMC=P1^3 ; sbit CS5460ADout = P1^0;//at RC1_bit; // INPUT PIN sbit CS5460AClk = P1^1; // at RC0_bit; // OUTPUT PIN sbit CS5460ADin = P1^2;//at RD0_bit; // OUTPUT PIN sbit buzzer=P2^7; //************************************************** *********** //CS5460A register addresses //************************************************** *********** #define CS5460A_REG_CONFIG 0x00 #define CS5460A_REG_AIN1_OFFSET 0x02 #define CS5460A_REG_AIN1_GAIN 0x04 #define CS5460A_REG_AIN2_OFFSET 0x06 #define CS5460A_REG_AIN2_GAIN 0x08 #define CS5460A_REG_CYCLE 0x0A #define CS5460A_REG_PULSE 0x0C #define CS5460A_REG_CURRENT 0x0E #define CS5460A_REG_VOLT 0x12 #define CS5460A_REG_POWER 0x14 #define CS5460A_REG_ENERGY 0x16 #define CS5460A_REG_IRMS 0x18 #define CS5460A_REG_VRMS 0x1A #define CS5460A_REG_TBC 0x1C #define CS5460A_REG_PWR_OFFSET 0x1E #define CS5460A_REG_STATUS 0x20 #define CS5460A_REG_IACOFFSET 0x22 #define CS5460A_REG_VACOFFSET 0x24 #define CS5460A_REG_MASK 0x34 #define CS5460A_REG_CONTROL 0x38 //************************************************** *********** //CS5460A commands //************************************************** *********** #define CONVERT_CONTINOUS 0xE8 #define CONVERT_ONCE 0xE0 #define CS5460A_SYNC0 0xFE #define CS5460A_SYNC1 0xFF #define POWER_UP_HALT 0xA0 #define CAL_AIN1_DC_GAIN 0xCA #define CAL_AIN1_DC_OFFSET 0xC9 #define CAL_AIN2_DC_GAIN 0xD2 #define CAL_AIN2_DC_OFFSET 0xD1 #define CS5460A_WRITE 0x40 #define CS5460A_READ 0x3E //************************************************** *********** //config data //************************************************** *********** #define CLOCK_DIVIDER 0x00000004 #define AIN1_GAIN_10 0x00000000 #define AIN1_GAIN_50 0x00010000 #define SOFT_RESET 0x00000080 #define MASK_NO_INTERRUPTS 0x00000000 #define STATUS_CLR_CRDY 0x00100000 #define STATUS_CLR_DRDY 0x00800000 #define STATUS_CLR_ALL 0x00FFFFFF #define CONTROL_DEFAULT 0x00000000 #define CURRENT_GAIN_DEFAULT 0x00100000 #define VOLT_GAIN_DEFAULT 0x00100000 #define COMPUTATION_CYCLE 0x00000FA0 #define TIME_BASE 0x00800000 extern long CS5460ARead24Bits(char AdcRegisterAddress); extern void CS5460AWriteCommand(char Command); extern void CS5460AReset(void); extern void CS5460AWrite24Bits(long RegisterData, char AdcRegisterAddress); extern void CS5460AReset(void); extern void CS5460APulseClock(void); void Delay_ms(unsigned int itime); unsigned long Va, Gain, Test, Volt ,energy; //================================================== ========================= //================================================== ========================= //char CS5460AReadDRDYBit(void) //{ //char ReadyBit; //long StatusRegister; //StatusRegister=CS5460ARead24Bits(CS5460A_REG_STATUS); //if(StatusRegister & 0x00800000) //{ //ReadyBit=1; //} //else //{ //ReadyBit=0; //}//get DRDY bit //return(ReadyBit); //} //================================================== ========================= char CS5460AReadCRDYBit(void) { char ReadyBit; long StatusRegister; StatusRegister=CS5460ARead24Bits(CS5460A_REG_STATUS); if(StatusRegister & 0x00100000) { ReadyBit=1; } else { ReadyBit=0; }///get CRDY bit return(ReadyBit); } //================================================== ========================= void CS5460AResetConfigure(void) { CS5460AReset(); CS5460AWrite24Bits((CLOCK_DIVIDER |AIN1_GAIN_10),CS5460A_REG_CONFIG);//Ain1 CS5460AWrite24Bits(CONTROL_DEFAULT,CS5460A_REG_CONTROL);//write default CS5460AWrite24Bits(STATUS_CLR_ALL,CS5460A_REG_STATUS);//clear all status CS5460AWrite24Bits(MASK_NO_INTERRUPTS,CS5460A_REG_MASK);//no interrupts //gain is 10, clock divider is 4 CS5460AWrite24Bits(CURRENT_GAIN_DEFAULT,CS5460A_REG_AIN1_GAIN);//Ain1 gain CS5460AWrite24Bits(VOLT_GAIN_DEFAULT,CS5460A_REG_AIN2_GAIN);//Ain2 gain CS5460AWrite24Bits(COMPUTATION_CYCLE,CS5460A_REG_CYCLE);//rms cycle CS5460AWrite24Bits(TIME_BASE,CS5460A_REG_TBC);//default time base CS5460AWriteCommand(CONVERT_CONTINOUS);//start conversions return; } //================================================== ========================= void CS5460AReset(void) { char BitCounter; for(BitCounter=0;BitCounter<3;BitCounter++)//send 3 SYNC1 bytes { CS5460AWriteCommand(CS5460A_SYNC1); } CS5460AWriteCommand(CS5460A_SYNC0);//send 1 SYNC0 byte CS5460AWrite24Bits(SOFT_RESET,CS5460A_REG_CONFIG);//issue soft reset } //================================================== =========================void CS5460AWriteCommand(char Command) { char WriteByte; char BitCounter; WriteByte=Command; for(BitCounter=0;BitCounter<8;BitCounter++) { if(WriteByte & 0x80) { CS5460ADin=1; } else { CS5460ADin=0; } CS5460APulseClock(); WriteByte=WriteByte<<1; } } //================================================== ========================= void CS5460AWrite24Bits(long RegisterData, char AdcRegisterAddress) { char BitCounter; char WriteByte; long WriteLong; WriteLong=RegisterData; WriteByte=(CS5460A_WRITE | AdcRegisterAddress);//write command with reg address for(BitCounter=0;BitCounter<8;BitCounter++)//send command { if(WriteByte & 0x80) { CS5460ADin=1; } else{CS5460ADin=0; } CS5460APulseClock(); WriteByte=WriteByte<<1; } for(BitCounter=0;BitCounter<24;BitCounter++)//write 24 bits { if(WriteLong & 0x00800000) { CS5460ADin=1; } else { CS5460ADin=0; } CS5460APulseClock(); WriteLong=WriteLong<<1; } return; } //================================================== ========================= long CS5460ARead24Bits(char AdcRegisterAddress) { char BitCounter=0; //send read command with register address long WriteByte; long Data24Bits; long ThreeSync0; WriteByte=CS5460A_READ & AdcRegisterAddress; Data24Bits=0; ThreeSync0=0x00FEFEFE; CS5460ADout=1;//pull up port pin for a read for(BitCounter=0;BitCounter<8;BitCounter++) { if(WriteByte & 0x80) { CS5460ADin=1; } else { CS5460ADin=0; } CS5460APulseClock(); WriteByte=WriteByte<<1; } for(BitCounter=0;BitCounter<24;BitCounter++)//read 24 bits { if(ThreeSync0 & 0x00800000) { CS5460ADin=1; } else { CS5460ADin=0; }//SYNC0 command on SDI CS5460APulseClock(); ThreeSync0=ThreeSync0<<1; if(CS5460ADout) { Data24Bits=((Data24Bits<<1)| 0x1); }//get msb and other bits else { Data24Bits=Data24Bits<<1; } } return(Data24Bits>>1 & 0x00FFFFFF); } //================================================== ========================= void CS5460APulseClock(void) { CS5460AClk=1; CS5460AClk=0; } //================================================== ========================= void Delay_ms(unsigned int itime) { unsigned int i,j; for(i=0;i<itime;i++) for(j=0;j<1257;j++) ; } void main(void) { //CS5460AResetMC=0; //Delay_ms(140);//delay //CS5460AResetMC=1; //unsigned long tp=0x0022; IE=0x82; // enable ext int 0 IT0=1; TMOD= 0x02; // timer 0 TH0=0x46; // for refresh led TR0=1; P0=0x00; P2 = 0x00; dig_num= 0; COUNT = 0; CS5460AClk=0; CS5460AResetConfigure(); Delay_ms(10); if(CS5460AReadCRDYBit()) { Va=1; } Gain=CS5460ARead24Bits(CS5460A_REG_VOLT);//dummy read //tp=0x1234; //display_data(1234); Delay_ms(140); while(1) { if(CS5460AReadCRDYBit()) { //CS5460AWrite24Bits(STATUS_CLR_DRDY,CS5460A_REG_STATUS);//clear DRDY Test=CS5460ARead24Bits(CS5460A_REG_CURRENT); Volt=CS5460ARead24Bits(CS5460A_REG_VOLT); energy = CS5460ARead24Bits(CS5460A_REG_POWER); CS5460AWrite24Bits(STATUS_CLR_CRDY,CS5460A_REG_STATUS);//clear CRDY } buzzer=0; display_data(1234); Delay_ms(140); display_data(Test); Delay_ms(140); display_data(Volt); Delay_ms(140); display_data(energy); Delay_ms(140); }//end of while }//end of main
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?