0% found this document useful (0 votes)
17 views

code

C code interview

Uploaded by

Kumari Mugilan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

code

C code interview

Uploaded by

Kumari Mugilan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

/*

* File: newmain.c

* Author: kumari.sd

* Created on 25 May, 2023, 2:56 PM

*/

#include <xc.h>

#include <xc.h>

#include <pic18f25k80.h>

#define _XTAL_FREQ 20000000

// PIC18F66K80 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1L

#pragma config RETEN = OFF // VREG Sleep Enable bit (Ultra low-power regulator is Disabled
(Controlled by REGSLP bit))

#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power
mode during Sleep)

#pragma config SOSCSEL = DIG // SOSC Power Selection and mode Configuration bits (Digital
(SCLKI) mode)

#pragma config XINST = OFF // Extended Instruction Set (Disabled)

// CONFIG1H

#pragma config FOSC = INTIO2 // Oscillator (Internal RC oscillator)

#pragma config PLLCFG = OFF // PLL x4 Enable bit (Disabled)

#pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Disabled)

#pragma config IESO = OFF // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L

#pragma config PWRTEN = OFF // Power Up Timer (Disabled)

#pragma config BOREN = OFF // Brown Out Detect (Disabled in hardware, SBOREN disabled)

#pragma config BORV = 3 // Brown-out Reset Voltage bits (1.8V)

#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is
selected)

// CONFIG2H

#pragma config WDTEN = OFF // Watchdog Timer (WDT disabled in hardware; SWDTEN bit
disabled)

#pragma config WDTPS = 1048576 // Watchdog Postscaler (1:1048576)

// CONFIG3H

#pragma config CANMX = PORTB // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and
RB3, respectively)

//#pragma config T0CKMX = PORTB // Timer0 Clock Input Mux bit (Timer0 gets its clock input from
the RB5/T0CKI pin on 64-pin packages)

//#pragma config T3CKMX = PORTG // Timer3 Clock Input Mux bit (Timer3 gets its clock input from
the RG2/T3CKI pin on 64-pin packages)

#pragma config MSSPMSK = MSK7 // MSSP address masking (7 Bit address masking mode)

#pragma config MCLRE = OFF // Master Clear Enable (MCLR Disabled, RE3 Enabled)

// CONFIG4L

#pragma config STVREN = ON // Stack Overflow Reset (Enabled)

#pragma config BBSIZ = BB2K // Boot Block Size (2K word Boot Block size)

// CONFIG5L

#pragma config CP0 = OFF // Code Protect 00800-03FFF (Disabled)

#pragma config CP1 = OFF // Code Protect 04000-07FFF (Disabled)


#pragma config CP2 = OFF // Code Protect 08000-0BFFF (Disabled)

#pragma config CP3 = OFF // Code Protect 0C000-0FFFF (Disabled)

// CONFIG5H

#pragma config CPB = OFF // Code Protect Boot (Disabled)

#pragma config CPD = OFF // Data EE Read Protect (Disabled)

// CONFIG6L

#pragma config WRT0 = OFF // Table Write Protect 00800-03FFF (Disabled)

#pragma config WRT1 = OFF // Table Write Protect 04000-07FFF (Disabled)

#pragma config WRT2 = OFF // Table Write Protect 08000-0BFFF (Disabled)

#pragma config WRT3 = OFF // Table Write Protect 0C000-0FFFF (Disabled)

// CONFIG6H

#pragma config WRTC = OFF // Config. Write Protect (Disabled)

#pragma config WRTB = OFF // Table Write Protect Boot (Disabled)

#pragma config WRTD = OFF // Data EE Write Protect (Disabled)

// CONFIG7L

#pragma config EBTR0 = OFF // Table Read Protect 00800-03FFF (Disabled)

#pragma config EBTR1 = OFF // Table Read Protect 04000-07FFF (Disabled)

#pragma config EBTR2 = OFF // Table Read Protect 08000-0BFFF (Disabled)

#pragma config EBTR3 = OFF // Table Read Protect 0C000-0FFFF (Disabled)

// CONFIG7H

#pragma config EBTRB = OFF // Table Read Protect Boot (Disabled)


#define swt PORTCbits.RC0 // two switches

void ext_interrupt(void);

#define I2C_WRITE 0

#define I2C_READ 1

unsigned int i2cReadData = 0;

unsigned int l_address = 0;

unsigned int j = 0;

// Initialise MSSP port.

void i2c_Init(void)

// Initialise I2C MSSP

// Master 100KHz

TRISCbits.TRISC3=1; // set SCL and SDA pins as inputs

TRISCbits.TRISC4=1;
SSPSTAT |= 0x80; /* Slew rate disabled */

SSPCON1 = 0x28; /* SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) */

SSPADD = 0x49; /* 100Khz @ 4Mhz Fosc */

//SSPADD = 0x19; /* 100Khz @ 4Mhz Fosc */

TRISCbits.TRISC2=0;

PORTCbits.RC2=0;

//SSP1CON1 = 0b00101000; // I2C enabled, Master mode

// SSP1CON2 = 0x00;

// SSP1CON3bits.SDAHT = 1;

// I2C Master mode, clock = FOSC/(4 * (SSPADD + 1))

// SSP1ADD = 39; // 100Khz @ 16Mhz Fosc

// SSP1ADD = 39;

// SSP1STAT = 0b11000000; // Slew rate disabled

// i2c_Wait - wait for I2C transfer to finish

void i2c_Wait(void){

while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) );

// i2c_Start - Start I2C communication


void i2c_Start(void)

i2c_Wait();

SEN=1;

// while(SEN); /* automatically cleared by hardware */ //ADDED

// i2c_Restart - Re-Start I2C communication

void i2c_Restart(void){

i2c_Wait();

RSEN=1;

// i2c_Stop - Stop I2C communication

void i2c_Stop(void)

i2c_Wait();

PEN=1;

// while(PEN); /* Wait for stop condition to finish */

// i2c_Write - Sends one byte of data

void i2c_Write(unsigned int data)

i2c_Wait();

SSPBUF = data;

}
// i2c_Address - Sends Slave Address and Read/Write mode

// mode is either I2C_WRITE or I2C_READ

void i2c_Address(unsigned int address, unsigned int mode)

l_address=address<<1;

l_address+=mode;

i2c_Wait();

SSPBUF = l_address;

// i2c_Read - Reads | byte from Slave device

unsigned char i2c_Read(unsigned char ack)

// Read data from slave

// ack should be 1 if there is going to be more data read

// ack should be 0 if this is the last byte of data read

i2c_Wait();

RCEN=1;

i2c_Wait();

i2cReadData = SSPBUF;

i2c_Wait();

if ( ack ) ACKDT=0; // Ack

else ACKDT=1; // NAck

ACKEN=1; // send acknowledge sequence


return(i2cReadData);

void SendByteSerially(unsigned char Byte) // Writes a character to the serial port

while(!TX2IF); // wait for previous transmission to finish

TXREG2= Byte;

unsigned char ReceiveByteSerially(void) // Reads a character from the serial port

while(!RC2IF); // Wait for transmission to receive

return RCREG2;

void SendStringSerially(const unsigned char* st)

while(*st)

SendByteSerially(*st++);
}

void UART1_uitoa(unsigned int number, char* ascii_number)

unsigned int position=0;

unsigned int length=0 ;

unsigned int temp_int=number;

if (number==0)

*( ascii_number)='0';

*( ascii_number+1)='\0';

else

while (temp_int)

temp_int/=10;

length++;

for (position=length;position>0;position--)

*( ascii_number + position-1) = '0' + number % 10;

number /= 10;

*( ascii_number + length) = '\0';

}
void UART1_put_int(unsigned int number)

char ascii_number[ 11 ];

UART1_uitoa( number, ascii_number ) ;

SendStringSerially( ascii_number ) ;

#define ACQ_US_DELAY 5

#define IN 1

#define OUT 0

#define HIGH 1

#define LOW 0

unsigned int i2c_FDC_read_2(unsigned char address)

unsigned int data,read_byte2,read_byte3;


// Read one byte

i2c_Start(); // send Start

i2c_Address(0x2a, I2C_WRITE); // Send slave address - write operation

i2c_Write(address); // DATA WRITE

i2c_Restart(); // Restart

i2c_Address(0x2a, I2C_READ); // Send slave address - read operation

// read_byte = i2c_Read(1); // Read one byte

// read_byte1 = i2c_Read(1);

data= i2c_Read(1);

data <<= 8;

data |= i2c_Read(0);

// If more than one byte to be read, (0) should

// be on last byte only

// e.g.3 bytes= i2c_Read(1); i2c_Read(1); i2c_Read(0);

i2c_Stop();

return data;// send Stop

unsigned int a,b,c;

void main(void) {

unsigned char ch;

unsigned int counter,i=0,j=0,n=30,k,e,lh=0,ll=0,x=0,y=0,z=0,t=0,g=0,h=0,m=0,u=0,devid;

static int s=0;


TXSTA2=0b00100100;

RCSTA2=0b10010000;// TX Pin

BAUDCON1=0b00001000;

SPBRG2=25; //51

T1CON=0X00;

TMR1H=0;

TMR1L=0;

TX2IE = 0; // Disable tx interrupts

RC2IE = 1; // Enable rx interrupts

//GIE = 1; // Enable global


interrupts

PIE3 = 0b00110000;

//i2c_Init();

SendStringSerially("Device Id:");

//devid=i2c_FDC_read_2(0x7f);

// UART1_put_int(devid);

// SendStringSerially("\n \r");

while(1)

SendStringSerially("ADC:");

//UART1_put_int(a);

//SendByteSerially('a');

//SendByteSerially('\r');

//SendByteSerially('\n');
/*if(RC0_bit == 0)

// while(MyButton == LOW)

lh++; ll=0; SendStringSerially("LH= ");

UART1_put_int(lh);

SendStringSerially("\r \n");

else if(RC0_bit == HIGH)

//while(MyButton == HIGH)

ll++; lh=0; SendStringSerially("LL= ");

UART1_put_int(ll);

SendStringSerially("\r \n");

*/

// ll=PORTCbits.RC7;

// SendStringSerially("LL= ");

//UART1_put_int(ll);
// SendStringSerially("\r \n");

You might also like