Skip to content

Instantly share code, notes, and snippets.

@sapher
sapher / pic16f1705_dht11.c
Last active August 29, 2015 14:18
Example of using a DHT11 sensor with a PIC16F1704. We use Timer2 to handle the timing part.
/******************************************************************************/
/* This example is running on a PIC16F1704 @ 4Mhz INTOSC */
/* To use it you need to configure your OSC and other things. */
/* The SIGNAL pin of the DHT11 is wired to the RA4 pin */
/* We add a little margin for all timing (~) */
/* Some information about the DHT11 : */
/* http://akizukidenshi.com/download/ds/aosong/DHT11.pdf */
/******************************************************************************/
#include <xc.h>
@sapher
sapher / main.m
Created January 19, 2015 09:23
Objective-C - Hello world
#import <Foundation/Foundation.h>
int main (void) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello World!\n");
[pool release];
return 0;
}
@sapher
sapher / GNUmakefile
Created January 19, 2015 09:16
GNUMakefile
include $(GNUSTEP_MAKEFILES)/common.make
# make a simple program in Objective-C
TOOL_NAME = App
# The implementation Objective-C file which is going to be compiled
App_OBJC_FILES = main.m
# Header files of your project
#App_HEADER_FILES = xxx.h
# Define compilation flags
ADDITIONAL_CPPFLAGS = -Wall -Wno-import
# Include rules for creating a command line tool for Objective-C
@sapher
sapher / map_pic.c
Last active March 5, 2019 20:14
Map function for PIC microcontroller that map a value from a range to another range
uint16_t map(uint16_t value, uint16_t fS, uint16_t fE, uint16_t tS, uint16_t tE) {
return ((long)(value - fS) * (long)(tE - tS)) / ((fE - fS) + tS);
}
// map(512, 0, 1024, 0, 512); > 127
@sapher
sapher / pic16f1704_usart.c
Last active August 29, 2015 14:09
PIC16F1704 USART TX example (RX is not implemented)
#include "xc.h"
#include "config.h"
#define BUFSIZE 12 //Maximum size of received string
volatile unsigned char rx_data;
volatile unsigned char rx_index;
volatile unsigned char rx_buffer[BUFSIZE];
/**
@sapher
sapher / pic12f615.c
Created November 13, 2014 12:56
PIC12F615 PWM example on AF pin GP5
#include "xc.h"
#include "config.h"
#define _XTAL_FREQ 8000000
/* Variables */
unsigned int i;
void setDuty(unsigned int duty) {
CCPR1L = duty >> 2;
@sapher
sapher / pic12f615_adc_interrupt.c
Last active April 27, 2023 14:46
PIC12F615 ADC interrupt exemple
#include "xc.h"
#include "config.h"
#define _XTAL_FREQ 4000000
unsigned short pot = 0;
void interrupt isr() {
// Disable GIE (best practice?)
@sapher
sapher / map.c
Last active August 29, 2015 14:08
C simple function that map a value from a range to another range
#include <stdio.h>
int map(int value, int fromStart, int fromEnd, int toStart, int toEnd) {
return (value - fromStart) * (toEnd - toStart) / (fromEnd - fromStart) + toStart;
}
int main(void) {
int x = map(50, 0, 100, 0, 255);
@sapher
sapher / sntp-client.c
Last active August 29, 2015 14:07
sntp client with bsd socket (unix)
//Includes
#include <stdio.h>
#include <stdlib.h> // EXIT_xxxxx
#include <string.h> //memset
#include <unistd.h> //close
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
//SNTP RFC > https://tools.ietf.org/html/rfc4330
@sapher
sapher / main.c
Last active August 29, 2015 14:07
Reading of 96bits long Unique ID on STM32F407VG
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include "STM32F4xx.h"
/* Private variables ---------------------------------------------------------*/
struct __FILE { int handle; };
FILE __stdout;
FILE __stdin;
/* Private functions ---------------------------------------------------------*/