PICSim 0.8.1
PICsim - PIC Simulator
Loading...
Searching...
No Matches

Introduction

This is the documentation of using the picsim simulation library functions.

Library functions usage simple example

Demonstration of a simple example of using picsim library functions

picsim_simple.c

#include<picsim/picsim.h>
#include<stdio.h>
int main()
{
int i;
unsigned char val;
//microcontroller object
_pic pic1;
//set serial port
pic_set_serial(&pic1,0, "/dev/tnt2",0,0,0);
//pic_set_serial(&pic1,"COM2",0,0,0);
//initialization and program loading
pic_init(&pic1, getprocbyname("PIC16F628A"), "../../../examples/shift/shift.hex", 1, 4e6);
//enable print messages of internal instruction execution (slow down the simulation speed)
pic1.print=1;
//loop
for(i=0;i<1000;i++)
{
//execute one instruction cycle
pic_step(&pic1);
//read digital pin 1
val=pic_get_pin(&pic1, 1);
//write digital pin 2
pic_set_pin(&pic1, 2, val);
}
//free internal memory
pic_end(&pic1);
}
int pic_set_pin(_pic *pic_, unsigned char pin, unsigned char value)
Set digital value of one pin.
int pic_init(_pic *pic_, int processor, const char *fname, int leeprom, float freq)
Initialize one pic object.
void pic_end(_pic *pic_)
finalize and free pic object internal memory
unsigned char pic_get_pin(_pic *pic_, unsigned char pin)
Get digital value of one pin.
unsigned int getprocbyname(const char *str)
Return processor ID by name.
void pic_step(_pic *pic_)
Execute one simulation step.
int pic_set_serial(_pic *pic_, int nser, const char *name, int flowcontrol, int ctspin, int rtspin)
Configure the serial port connection.
Definition picsim.h:281
unsigned char print
print information ON/OFF
Definition picsim.h:282

Makefile

CC= gcc
FLAGS= -Wall -ggdb
all: picsim_simple.c
$(CC) $(FLAGS) picsim_simple.c -o picsim_simple -lpicsim
clean:
rm -rf picsim_simple

Library functions usage realtime example

Demonstration of a realtime Linux example of using picsim library functions

realtime.c

#include <picsim/picsim.h>
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#define FREQ 4000000L
static _pic pic1;
void timer_callback(int signum)
{
unsigned char PORTB;
static unsigned char PORTB_old;
const unsigned int nsteps = 100e-3 / (4.0 / pic1.freq);
for (int i = 0; i < nsteps; i++)
{
pic_step(&pic1);
//READ pins
// PORTB |= pic_get_pin(&pic1, 6);
// use direct access instead pic_get_pin to speed up
PORTB = 0;
PORTB |= pic1.pins[6 - 1].value; // RB0
PORTB |= pic1.pins[7 - 1].value << 1; // RB1
PORTB |= pic1.pins[8 - 1].value << 2; // RB2
PORTB |= pic1.pins[9 - 1].value << 3; // RB3
PORTB |= pic1.pins[10 - 1].value << 4; // RB4
PORTB |= pic1.pins[11 - 1].value << 5; // RB5
PORTB |= pic1.pins[12 - 1].value << 6; // RB6
PORTB |= pic1.pins[13 - 1].value << 7; // RB7
if (PORTB != PORTB_old)
{
PORTB_old = PORTB;
printf("PORTB =0x%02X\n", PORTB);
}
}
}
void ctrlc_callback(int signum)
{
printf("end\n");
pic_end(&pic1);
exit(0);
}
int main()
{
pic_init(&pic1, getprocbyname("PIC16F628A"), "../../../examples/shift/shift.hex", 1, FREQ);
struct itimerval new_timer;
struct itimerval old_timer;
new_timer.it_value.tv_sec = 0;
new_timer.it_value.tv_usec = 100 * 1000; //100ms
new_timer.it_interval.tv_sec = 0;
new_timer.it_interval.tv_usec = 100 * 1000; //100ms
setitimer(ITIMER_REAL, &new_timer, &old_timer);
signal(SIGALRM, timer_callback);
signal(SIGINT, ctrlc_callback);
// Waitting forever
while (sleep(10))
;
}
float freq
frequency
Definition picsim.h:311
unsigned char value
value
Definition picsim.h:96

Makefile

CC= gcc
FLAGS= -ggdb -Wall
all: realtime
realtime: realtime.c
$(CC) $(FLAGS) realtime.c -o realtime -lpicsim
clean:
rm -rf realtime